Stuck on the Django Yellow Page? Here are 13 Solutions

July 7, 2020, 3:46 p.m.

Stuck on the Django Yellow Page? Here are 13 Solutions

Let's face it, refreshing your browser window and getting the Django Yellow Page error can be frustrating when first learning to use the web framework.

Even worse is an error in the CLI or terminal that seems to provide little insight on how to solve the issue.

We always advise you to look at the exception type, value, and location along with the Traceback, but not every error page is easy to read when first learning to code.

Learning to read these errors and quickly identify the solution comes with practice, but in the beginning, it may require a few hours dedicated to googling errors and searching through Stack Overflow questions to understand the one line of code you forgot to add that caused the error.

So we decided to break down common errors you're bound to run into when learning Django and using the common Django package django-crispy-forms.

 

Possible errors during Django initial installation and configuration

 

1. TERMINAL: Can't open file 'manage.py': [Errno 2] No such file or directory

If you try to create a Django application but forget to enter into the project folder, in this case, mysite, you will get the error stating that there is no manage.py file. That's because you can only run manage commands within the project folder containing manage.py

To solve this issue, enter into the project folder mysite folder, then run the startapp command again. 

macOS Terminal

(env)User-Macbook:env user$ django-admin startproject mysite

(env)User-Macbook:env user$ cd mysite

(env)User-Macbook:mysite user$ python3 manage.py startapp main

Windows Command Prompt

(env)C:\Users\Owner\desktop\env> django-admin startproject mysite

(env)C:\Users\Owner\desktop\env> cd mysite

(env)C:\Users\Owner\desktop\env\mysite> py manage.py startapp main

 


 

2. BROWSER WINDOW: Django rocket page "The install worked successfully! Congratulations!" even after adding configurations

You have not configured your URL files correctly if you have edited the Django project but still see the Django rocket page.

Django documentation suggests your project contains two urls.py files.

The first file is by default in the project folder, while the other file needs to be created in the application folder. 

The two URL files should look as follows:

env > mysite > main > (New File) urls.py

from django.urls import path
from . import views

app_name = "main"   


urlpatterns = [
    path("", views.homepage, name="homepage"),
]

You need to create a urls.py file in your application folder and specify the app's name, along with a path to a template, such as path(" ", views.homepage, name="homepage")

 

env > mysite > mysite > urls.py

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include  #add include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include ('main.urls')),   #add this
]

Then in the existing URLs file, mysite > urls.py, you need to add path(' ', include ('main.urls')), that connects this file to main > urls.py.

With these two files configured correctly, the error will be resolved. 

 


 

3. BROWSER WINDOW & TERMINAL: "This site can't be reached" -- NameError: name 'include' is not defined

A NameError means you are calling a name that is not defined. 

env > mysite > mysite > urls.py

from django.contrib import admin
from django.urls import path, include  <----

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include ('main.urls')),
]

To resolve this particular error, you need to go to your project's URL file, in this case, mysite > urls.py and import include from django.urls at the top of the file. 

 


 

4. TERMINAL: AttributeError: model 'main.views' has no attribute 'homepage'

Next, this AttributeError indicates you have not added a view function or class to your project that connects the HTML template to the URL path.

env > mysite > mysite > views.py

from django.shortcuts import render

# Create your views here.
def homepage(request):
	return render(request = request, template_name="main/home.html")

If you are looking to write a function-based view define your function as homepage, the same view function specified in the urls.py path. Then like any other Python function, you will need to add a return statement that returns a GET request and the name of the template you wish to render. 

 


 

5. BROWSER WINDOW: TemplateDoesNotExist at/ main/home.html

If you forgot to add the application to the list of installed apps in your settings OR you have not created the template file you refer to in the views, you will get this error. In this case, we are calling for a home.html file in the view function we have yet not created. 

env > mysite > main > (New Folder) templates > (New Folder) main > (New File) home.html

<p>Hello world!</p>

If you haven't already, create a new folder structure with templates > main folders within your app, in this case main. Then create an HTML template file, the same file called in the view function. Add some HTML so when the page renders, you can see it is properly connected.

For reference, we are taking this folder structure from the Django documentation. 

 

Now, if you have your HTML template but are still getting this error, you need to make sure your app is added to the list of INSTALLED_APPS in the settings file. 

env > mysite > mysite > settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',   <-----
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Add 'main.apps.MainConfig' to the list of installed apps. The order does not matter. 

 


 

6. TERMINAL: You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.

When you run the server with the command python3 manage.py runserver or py manage.py runserver for Windows, there is a message stating there are unapplied migrations that could affect your project's functionality. 

macOS Terminal

(env)User-Macbook:mysite user$ python3 manage.py migrate

Windows Command Prompt

(env)C:\Users\Owner\desktop\env\mysite> py manage.py migrate

To make the initial migrations run the command python3 manage.py migrate for mac or py manage.py migrate for Windows. 

 


 

When working with packages

Installing packages is a large part of working with the Django web framework and customizing the project to your needs. One of the most common packages used is django-crispy-forms, a Python package designed to quickly add style to Django forms.

We have chosen to work with this package as the example given that it not only has a similar installation process to most Python packages but also has a few less common configurations that can cause some confusing errors. 

 

7. BROWSER WINDOW: TemplateSyntaxError at/ Invalid filter: 'crispy'

If you forget to load the crispy form tag at the top of the HTML template but call the crispy for filter on the form, you will get this error.

env > mysite > main > templates > main > home.html

{% load crispy_forms_tags %}
	
		<form method="post">
        {% csrf_token %}
            {{form|crispy}}
            <button type="submit">Submit</button>
        </form>
    

Include the django-crispy-form tag at the top of the HTML template. 

 


 

8. BROWSER WINDOW: TemplateSyntaxError at/ Invalid filter:'crispy'

This is one of the trickier errors given that it does not have much to do with the actual filter added to the form.

The error occurs when you have a space between the closing percentage sign and bracket of the crispy form tags like so: {% load crispy_forms_tags % }.

env > mysite > main > templates > main > home.html

{% load crispy_forms_tags %}
	
		<form method="post">
        {% csrf_token %}
            {{form|crispy}}
            <button type="submit">Submit</button>
        </form>
    

It's honestly one of the harder errors to resolve if you are new to using crispy-forms or Jinja.

You just need to make sure there are no spaces in the actual Jinja tag {% ... %}. But space between the tag and the variables within them is okay. 

 


 

9. BROWSER WINDOW: TemplateSyntaxError at/ 'crispy_form_tags' is not registered tag library. Must be on of:

admin_list

admin_modify

admin_urls

cache

crispy_forms_field

crispy_forms_filters

crispy_forms_tags

...

 

This error occurs when you misspell the tag loaded at the top of the file. As you can see, the available tags are listed below, one of which is the correct tag crispy_forms_tags.

env > mysite > main > templates > main > home.html

{% load crispy_forms_tags %}
	
		<form method="post">
        {% csrf_token %}
            {{form|crispy}}
            <button type="submit">Submit</button>
        </form>
    

Be careful and make sure you spell the filters and tags correctly. A good way to avoid this error is to copy and paste the tags directly from the documentation. 

 

If you are sure you have spelled the tag correctly but are still getting this error, make sure you have added crispy_forms to the list of INSTALLED_APPS in the settings file. 

env > mysite > mysite > settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',               <----
]

Again, be careful of spelling errors. Often the errors you get when using a package involve a small spelling error. 

 


 

10. TERMINAL: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'

This error occurs when you misspell the INSTALLED_APP crispy_forms.

env > mysite > mysite > settings.py

 

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',               <----
]

 

You need to pay attention to the exact spelling and formatting of the installed app or copy it directly from the documentation to avoid this error.

 


 

11. BROWSER WINDOW & TERMINAL: No errors BUT the form isn't crispy!

If you think you have made all of the necessary configuration changes but the form is still not crispy, you may have forgotten to add the template pack correctly.

Some packages have template packs or configurations settings that need to be added to the settings file in addition to the installed app. 

env > mysite > mysite > settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
]

CRISPY_TEMPLATE_PACK = 'uni-form'

For django-crispy-forms, there is a template pack that needs to be added to the settings.py file. For the default template setting, you need to use uni-form. If you are using Bootstrap, bootstrap4 is the name of the template pack you need to add.

 


 

12. BROWSER WINDOW: TemplateDoesNotExist at/ uni-form/uni_form.html

If you are using the Bootstrap CSS framework in your Django project, you need to call the correct crispy template pack or you will get this error. 

env > mysite > mysite > settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

The pack is bootstrap4all lowercase.

 


 

When working within Python files

The last errors we will discuss are the errors you get when working with Python files.

These are by far some of the most frustrating errors given the formatting style and indentation levels required for Python. 

13. TERMINAL: IndentationError: unindent does not match any outer indentation level

Indentation errors always seem to occur in the views.py file given that it is arguably the most heavily edited Python file in a Django project. 

env > mysite > mysite > views.py

from django.shortcuts import render
from .forms import ContactForm

# Create your views here.
def homepage(request):
	 form = ContactForm ()
	return render(request = request, template_name="main/home.html", context={'form':form})

If you look at the example above, this code will give you the indentation error.

It may be hard to see at first, but the error is occurring because the second line is spaced slightly inward from the rest of the code within the function. 

Now the best way to handle this error if you are in Sublime Text Editor is to highlight the entire code, then click Edit > Line > Reindent. If your code editor does not have a reindent feature, unfortunately, you may need to go line by line reindenting the code yourself. 

But to avoid this error together be sure to only use the Tab and Enter keys, never the spacebar.

 


 

So overall, if you wish to avoid most of these errors you just need to follow the documentation, not make any spelling errors, and only use the Tab and Enter keys. 

That’s it.

But of course, it’s just not that simple. 

It’s hard to avoid spelling errors and no one really wants to sift through every page of documentation.

So the only thing you can really do is keep practicing and reading your way through the Traceback until those Django “Yellow pages” are nothing more than gentle reminders.


0
Subscribe now

Subscribe to stay current on our latest articles and promos





Post a Comment
Join the community

0 Comments