This tutorial covers how to create a static page (in this case an About page) in your Django project. Full accompanying code can be found on Github.

Setup

First we need to setup a virtual environment and install Django. If you need help with this, please see the following post on how to install Django, Python, virtualenv, virtualenvwrapper correctly.

$ mkvirtualenv django-about-page-three-ways
$ pip install django==1.10.2

Now let’s create a new Django project and confirm that the installation was successful:

$ django-admin startproject django_about_page_three_ways
$ python manage.py runserver

If you visit http://127.0.0.1:8000/ you should see the standard “Welcome to Django” page in light-blue pastel.

Templates folder and about.html file

There is often confusion over where to places a templates directory in a new Django project. The official Polls Tutorial recommends placing your templates within a dedicated templates folder for each individual app, however this quickly becomes unwieldy as a Django project grows in size.

A simpler approach advocated in Cookiecutter Django is to simply create a root-level templates folder for the entire project. That’s what we’ll do here.

Let’s update our settings.py file to change the location for our templates directory. By default, the location is set as follows:

# settings.py
'DIRS': [],

Update this line to the following:

# settings.py
'DIRS': [
        os.path.normpath(os.path.join(BASE_DIR, 'templates')),
    ],

Now create a new Templates folder and an about.html file within it:

$ mkdir templates
$ touch templates/about.html

In your text editor, add the following text to the about.html file:

# templates/about.html
<h1>About page</h1>

In Django, we need a Template, View, and URL to display a page. Our Template is done. Now let’s explore three different ways to configure our View and URL.

1) Function-Based View

The traditional way to define a view is through a function in a views.py file. Let’s create that file within the django_about_page_three_ways project directory:

$ touch django_about_page_three_ways/views.py

And now add the necessary logic:

# django_about_page_three_ways/views.py
from django.shortcuts import render


def about_page_view(request):
    return render(request, 'about.html')

We import render from django.shortcuts, a helper function included by default with Django. Then we create a new function called about_page_view with the required “request” argument and pass in the name of our desired template, about.html.

Next we need to update our urls.py file so it looks like the following:

# django_about_page_three_ways/urls.py
from django.conf.urls import url
from django.contrib import admin

from .views import about_page_view

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^about/',
        about_page_view, name='about'),
]

We’ve added a line to import the about_page_view by name from the views.py file. And then create a new URL pattern at “/about/”.

Run the internal Django server to see our completed page:

$ python manage.py runserver

You will see our simple “About page” text at http://127.0.0.1:8000/about/.

2) Class Based View

Django includes several generic class-based views that are easy to extend and customize. We can use the TemplateView for our About page.

Update the views.py file with the following lines:

# django_about_page_three_ways/views.py
from django.views.generic import TemplateView


class AboutPageView(TemplateView):
    template_name = 'about.html'

We have imported TemplateView from Django’s generic display views and then created a new view, AboutPageView.

Next we need to update our urls.py file:

# django_about_page_three_ways/urls.py
...
from .views import about_page_view, AboutPageView

urlpatterns = [
    ...
    url(r'^about1/',
        AboutPageView.as_view(), name='about1'),
]

We have added AboutPageView to our imported views and then created a new URL pattern at “/about1/”.

Run our server again:

$ python manage.py runserver

The About page will be displayed at http://127.0.0.1:8000/about1/.

3) URLS only approach

A third approach is to include our class-based view directly in the urls.py file. This is my preferred approach since we don’t really need a separate views.py file given we are not making any changes to TemplateView.

Within our urls.py file add the following code:

# django_about_page_three_ways/urls.py
from django.views.generic import TemplateView

urlpatterns = [
    ...
    url(r'^about2/',
        AboutPageView.as_view(), name='about2'),
]

To confirm this works, run the server and visit our new page at “/about2/”.

$ python manage.py runserver

The About page will be displayed at http://127.0.0.1:8000/about2/.

Next Steps

Check out Django for Beginners, a free online book on how to create and deploy multiple Django applications. Starting with a simple “Hello, World” application it progresses through multiple web applications of increasing complexity showing Django best practices along the way.