Django for Beginners /

Chapter 3: Personal Website

Course Contents

In this chapter, we will build a personal website with a homepage and an about page while learning more about Django's templates, function-based views, and testing. Templates are the presentation layer that controls how data is displayed, and they also allow basic logic consistent with Django's Don't Repeat Yourself (DRY) design philosophy. Views process incoming requests, contain application logic, and return responses—often by rendering a template. The third fundamental concept is testing, which is vital to any web project and well-supported in Django. By the end of this chapter, you will have learned how to work with simple function-based views and templates and write your first tests.

Initial Setup

Our initial setup is similar to the previous chapter and contains the following steps:

  • Make a new directory for our code called personal_website and navigate into it
  • Create a new virtual environment called .venv and activate it
  • Install Django
  • Start a new Django project called django_project
  • Add a new app called pages

Within a new command line shell, navigate to the code folder in your home directory and create a new folder called personal_website. You should not see an active virtual environment, represented by (.venv); if you do, you're still in an existing virtual environment, so type deactivate to leave it. Change directories into personal_website, create a new virtual environment, and activate it.

# Windows
$ cd ~\code
$ mkdir personal_website
$ cd personal_website
$ python -m venv .venv
$ .venv\Scripts\Activate.ps1
(.venv) $

# macOS
$ cd ~/code
$ mkdir personal_website
$ cd personal_website
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $

Next, install Django, create a new project called django_project, and make a new app called pages. These commands will become familiar by the end of this book; the only thing that changes is the name of your project or app.

(.venv) $ python -m pip install django~=6.1.0
(.venv) $ django-admin startproject django_project .
(.venv) $ python manage.py startapp pages

Even though we added a new app, Django will not recognize it until we update the INSTALLED_APPS setting within django_project/settings.py. Open your text editor and add it to the bottom now:

# django_project/settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "pages",  # new
]

Initialize the database with migrate and start the local web server with runserver.

(.venv) $ python manage.py migrate
(.venv) $ python manage.py runserver

Then, navigate to http://127.0.0.1:8000/ to see the Django welcome page. With a few commands, we have a fresh Django project running in a virtual environment and a pages app.

Homepage

A Django "view" is a Python function that accepts a web request and returns a web response. When a web page is requested, Django automatically creates an HttpRequest object with metadata about the request. The view returns an HttpResponse object.

We will begin by repeating the steps from the previous chapter to create a homepage using just a view and a URL dispatcher. Then, we will create an About page using a template and more complex view logic.

Let's start with the view. Django prepopulates the views.py file within an app with the following code:

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

# Create your views here.

We will use render for the About page, so leaving that import unchanged is fine. For our homepage, we will repeat the steps from the previous chapter: import the class HttpResponse, create a view named home_page_view, name the first parameter (which is always the HttpRequest object!) as request, and then return the text "Homepage."

# pages/views.py
from django.http import HttpResponse
from django.shortcuts import render


def home_page_view(request):
    return HttpResponse("Homepage")

The next step is creating a urls.py file within the pages app that imports path from django.urls, home_page_view from the views.py file in the local directory, and sets a route at the empty string, "", that calls home_page_view.

# pages/urls.py
from django.urls import path

from .views import home_page_view

urlpatterns = [
    path("", home_page_view),
]

The final step is updating the project-level urls.py file, the initial entry point for all URL requests. We import the include function to include other URL configurations and set a URL route of "" for the pages application.

# django_project/urls.py
from django.contrib import admin
from django.urls import include, path  # new

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("pages.urls")),  # new
]

That's it! If the local server is still running on the command line, you should be able to visit the homepage in your browser:

Homepage

Function-Based View About Page

render() is a Django shortcut function useful for working with a template. The first parameter in render() is the request object, and the second parameter is a template name. That means if we want to create a view for an about page using a template called about.html we can write it like this:

# pages/views.py
from django.http import HttpResponse
from django.shortcuts import render


def home_page_view(request):
    return HttpResponse("Homepage")


def about_page_view(request):  # new
    return render(request, "pages/about.html")

The new view is named about_page_view and has as its first parameter the HttpRequest object, which we've named request. It uses the render() function to combine the request object with a template named "pages/about.html" and return the result as an HttpResponse object.

Templates

Templates are Django's presentation layer, providing a convenient way to generate HTML files that can also include CSS, JavaScript, and media such as images. By default, Django's template loading engine looks for a "templates" subdirectory in each app. That means we could add a templates directory within the pages app and then include the about.html in the following way:

└── pages
    └── templates
        └── about.html

This approach works, but it isn't a best practice. Django picks the first one it finds based on INSTALLED_APPS order, which can lead to surprising bugs when two apps share template names. Fortunately, a simple fix is to namespace template files by placing them within another directory containing the application name. In other words: app -> templates -> app -> template file.

In our pages app, that would look like this:

└── pages
    └── templates
        └── pages
            └── about.html

As a Django best practice, always use this approach when storing template files within an app. You can create these new directories within your text editor or directly on the command line:

(.venv) $ mkdir pages/templates
(.venv) $ mkdir pages/templates/pages

Then, add a new file called about.html within the pages/templates/pages directory. The about.html file will have an <h1> tag for a headline and a <p> tag for paragraph text.

<!-- pages/templates/pages/about.html -->
<h1>About page</h1>
<p>This is the new template-powered About page.</p>

Our template is complete! The view, about_page_view, can access the template when called, but the remaining step is configuring a URL dispatcher.

URL Dispatcher

By now, the pattern for adding a new URL route should feel familiar. We import our view, about_page_view, and then set its path, about/, and the view name.

# pages/urls.py
from django.urls import path

from .views import about_page_view, home_page_view  # new

urlpatterns = [
    path("about/", about_page_view),  # new
    path("", home_page_view),
]

And that's it! Start up the development web server using the runserver command.

(.venv) $ python manage.py runserver

If you navigate to http://127.0.0.1:8000/about/, the About page will be visible.

FBV About Page

The Django Template Language

All web frameworks (including Django) need a way to generate HTML dynamically, and the most common approach is to use templates that contain static content and a special syntax for inserting dynamic content. Django has its own template language and isn't trying to replace dedicated JavaScript frontends like Vue, React, or Angular. Rather, it is a deliberately limited—but still powerful—way to use comments, variables, filters, tags, and template inheritance.

Let's revisit our about.html template to try out some of these features. We'll start with comment tags, which let us add notes for ourselves and other developers that won't appear on the rendered page. Django templates support two comment syntaxes. For short, single-line comments, wrap the text in {# ... #}.

<!-- pages/templates/pages/about.html -->
<h1>About page</h1>
<p>This is the new template-powered About page.</p>
{# This is just a comment. It won't appear on the web page! #}

For comments that span multiple lines, use the {% comment %} tag instead:

<!-- pages/templates/pages/about.html -->
<h1>About page</h1>
<p>This is the new template-powered About page.</p>
{% comment %}
This is a longer comment.
It can span as many lines as you want.
{% endcomment %}

Either way, save the file and refresh the web browser—the comment won't be visible. Django's template engine strips comments out when it renders the template, so they never make it into the HTML that the browser receives.

Broadly speaking, Django templates should not be complicated. It is OK to include basic logic, but heavier work belongs in views or models (which we will do later in the book).

Template Context

Whenever a Django template is rendered, a context is also created. This context is a dictionary-like object with variable names for keys and variable values as values. We can and will update this context with information, often from a database. When you render a template with a given context, every key in the context dictionary becomes a variable in the template that you can access and use.

The render() shortcut function expects parameters in the following order:

  1. The HttpRequest object, conventionally named request
  2. The template_name
  3. The context dictionary

In other words: render(request, template_name, context). By default, context is None, but we can add information to see it in action.

# pages/views.py
from django.http import HttpResponse
from django.shortcuts import render


def home_page_view(request):
    return HttpResponse("Homepage")


def about_page_view(request):
    context = {"name": "Alice"}  # new
    return render(request, "pages/about.html", context)  # new

Here, we've added a variable, context, containing a dictionary with the key "name" and the value "Alice." To display the context in our template, surround the variable name with double curly braces {{ }}. Since the key is "name," we type {{ name }} to make it visible.

<!-- pages/templates/pages/about.html -->
<h1>About page</h1>
<p>My name is {{ name }}.</p>

If you refresh your browser, it will now show the name "Alice."

About Page with Name

As an exercise, swap out the name "Alice" with your name in the pages/views.py file. Then refresh the web browser to see if it now appears on the web page.

We can also add other key/value pairs to the context. For example, let's add an "age."

# pages/views.py
from django.http import HttpResponse
from django.shortcuts import render


def home_page_view(request):
    return HttpResponse("Homepage")


def about_page_view(request):
    context = {
        "name": "Alice",
        "age": 33,  # new
    }
    return render(request, "pages/about.html", context)

To display the age on our web page, we can use the variable {{ age }} in the template. Note that the value of name, "Alice", is a string since it is in quotation marks, whereas 33 is an integer. Setting data types matters in Python code and Django applications, which we'll cover more when we introduce models.

<!-- pages/templates/pages/about.html -->
<h1>About page</h1>
<p>My name is {{ name }}. I am {{ age }} years old.</p>

Refresh the About page again to see it update.

About Page with Name and Age

In the pages/views.py file, update the age variable with your own age and refresh the web page to see the updated result. Anything we pass as a variable to our template context can be rendered on our web page.

The Django template language also comes with over eighty built-in tags and filters that provide additional functionality. We will explore them in greater depth later in the book.

Tests

Writing tests for your code is as important as writing the code itself. Without accompanying tests, you can't submit new code changes to an open-source project or within any well-structured company. The reasoning is simple: while your code change may be small, you never know what else it might inadvertently break in the project. Good tests build confidence in your codebase, don't take long, and can run automatically on new code changes. In the words of Django co-creator Jacob Kaplan-Moss, "Code without tests is broken by design."

Testing falls into two main categories: unit and integration. Unit tests check a piece of functionality in isolation, while integration tests check multiple linked pieces. Unit tests run faster and are easier to maintain since they focus on only a tiny amount of code. Integration tests are slower and harder to maintain because a failure doesn't point you to the specific cause. Most developers focus on writing many unit tests and fewer integration tests.

The next question is, what to test? Whenever you create new functionality, you need a test to confirm it works as intended. For example, in our project, we have a homepage and an about page, and we should test that both exist at the expected URLs. It may seem unnecessary now, but as a project grows, there's no telling what can change.

The Python standard library contains a built-in testing framework called unittest that uses TestCase instances and a long list of assert methods to check for and report failures.

Django's testing framework provides several extensions on top of Python's unittest.TestCase base class. These include a test client for making dummy web browser requests, several Django-specific additional assertions, and four test case classes: SimpleTestCase, TestCase, TransactionTestCase, and LiveServerTestCase.

Generally, use SimpleTestCase when you don't need a database, and TestCase when you do. TransactionTestCase is helpful to test database transactions directly, while LiveServerTestCase launches a live server thread for testing with browser-based tools like Selenium.

Note: You may have noticed that methods in unittest and django.test are written in camelCase rather than the more Pythonic snake_case pattern. This is because unittest is based on the JUnit testing framework in Java, which uses camelCase, so when Python added unittest, it came with camelCase naming.

If you look within our pages app, Django already provided a tests.py file we can use. Since our project doesn't involve a database, we'll import SimpleTestCase at the top of the file. For our first tests, we'll check that the two URLs for our website, the homepage and about page, return HTTP 200 status codes, the standard response for a successful HTTP request.

# pages/tests.py
from django.test import SimpleTestCase


class HomepageTests(SimpleTestCase):
    def test_url_exists_at_correct_location(self):
        response = self.client.get("/")
        self.assertEqual(response.status_code, 200)


class AboutpageTests(SimpleTestCase):
    def test_url_exists_at_correct_location(self):
        response = self.client.get("/about/")
        self.assertEqual(response.status_code, 200)

To run the tests, quit the server with Ctrl + C and type python manage.py test on the command line.

(.venv) $ python manage.py test
Found 2 test(s).
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

If you see an error such as AssertionError: 301 != 200, you likely forgot to add the trailing slash to "/about" above. Django notices the missing slash and automatically redirects to /about/—a feature controlled by its APPEND_SLASH setting—but that redirect is a 301 response, not a 200 success response!

Git and GitHub

It's time to track our changes with Git and push them to GitHub. We'll start by initializing our directory and checking the status of our changes.

(.venv) $ git init
(.venv) $ git status

We learned in the last chapter that by creating a .gitignore file, we can instruct Git to ignore certain files and directories. We will include the .venv directory containing our virtual environments. We'll also ignore the __pycache__ directory, which contains compiled bytecode, and the database itself, db.sqlite3. It's also best practice not to commit databases to source control because they grow quickly and, in real projects, can contain sensitive data. Large teams often need separate access levels for a project. Often, every developer can view the source code because Git lets you quickly roll back and fix mistakes. Rolling back changes to a production database is much harder, and most developers don't need access to it as long as they have a local or testing database available. So, it's best practice to keep database access separate from your source code, even with file-based databases like SQLite, so we'll do that here. Create a new .gitignore file and add the following three lines:

.venv/
__pycache__/
db.sqlite3

Run git status again to confirm these three entries are being ignored. We want a record of installed packages in our virtual environment, which we can do by creating a requirements.txt file.

(.venv) $ python -m pip freeze > requirements.txt

Run git status one final time to confirm that the newly created requirements.txt file is visible. Then, add all intended files and directories and include an initial commit message.

(.venv) $ git status
(.venv) $ git add -A
(.venv) $ git commit -m "initial commit"

Over on GitHub, create a new repo called "personal-website" and make sure to select the "Private" radio button. Then click on the "Create repository" button.

On the next page, scroll down to where it says "…or push an existing repository from the command line." Copy and paste the three commands there into your terminal.

It should look like the below, albeit instead of wsvincent as the username, it will be your GitHub username.

(.venv) $ git remote add origin https://github.com/wsvincent/personal-website.git
(.venv) $ git branch -M main
(.venv) $ git push -u origin main

Conclusion

Congratulations on building your second Django website. The complete source code for this chapter is available on GitHub for reference. Although the website is quite basic, we have explored several fundamental concepts, including function-based views, templates, URL dispatchers, and testing. We also used Git again, created requirements.txt and .gitignore files, and pushed our code to GitHub for the first time. With this foundation in place, we can move more quickly to the next chapter, where we will build a Company website using class-based views, template inheritance, and even more testing.