Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

Django Online Bookstore, Course Project

The best way to learn how to program is by creating a practical project. We will create a Django online bookstore app. Our application will have categories, authors, and books that visitors to our online store can browser and buy. Our application will use multiple applications. We will create an app for each component like categories, books, and authors. We will hook up the application to the database and do many cool things.

Topics to be covered

We will cover the following topics in this lesson

  • Course project development environment setup
  • How to create and register apps in Django
  • How URLs work in Django
  • Multiple applications URLs Best Practices
  • Lesson Challenge
  • Summary

Course project development environment setup

We will create our project in a virtual environment. If you not installed the virtual environment yet then you can run the following command

pip3 install virtualenv

HERE,

  • The above command creates a virtual environment for our project

Next we will create a virtual environment for our project

Run the following command

mkdir onlinestore && cd onlinestore && virtualenv venv

HERE,

  • The above command creates a new directory onlinestore, browsers into it then creates a virtual environment

Next we need to activate our virtual environment by running the following command

source venv/bin/activate

Once the virtual environment has been activated, you can check the version of python and pip that you have by running the following command

python --version
pip --version

Next we need to install Django into our virtual environment. Run the following command

pip install django

Once Django has successfully been installed, you can run the following command to create a project

django-admin startproject bookstore

Next we need to browse into our new project bookstore

cd bookstore

Finally we can start the built-in web server to make sure that everything is ok.

python manage.py runserver

You should be able to see the default Django 3 home page.

Django 3 Tutorial

How to create and register apps in Django

We can build the whole app in one place but it is better to separate it into apps based on the logic. Django lets us create multiple apps in the project, each dedicated to a part of the website. For example, if you have a website of a shop and blog, you can divide it into a shop app and blog app.

We will create three (3) apps namely;

  • books - it will handle all aspects of managing books
  • authors - it will handle all aspects of managing authors
  • categories - it will handle all aspects of managing categories

Run the following commands to create our apps

django-admin startapp authors
django-admin startapp books
django-admin startapp categories

HERE,

  • The above commands will create app directories authors, books, and categories in your project directory. We covered basics of apps in the previous lesson so we will not go into details in this lesson.

How to create and register apps in Django

Django keeps a list of installed apps in the settings.py file. You have to register your apps here if you want to use Django to automate tasks such as making migrations etc.

Open the file settings.py that is in the directory /bookstore/bookstore

Locate the following section

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Let's now add our new apps to the above list like so

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'authors',
    'books',
    'categories',
]

Now Django is aware of our applications that we created. It as simple as that.

How URLs work in Django

When a user requests for a resource for example, goes to the home page of our app, Django checks the definition of that route in the file urls.py and calls the necessary functions. Let's look at a practical example.

Create a new file views.py in the directory /bookstore/bookstore

Add the following code to views.py

from django.http import HttpResponse

def home_page(request):
    html = "<html><body><h1>Welcome to our Bookstore</h1></body></html>"
    return HttpResponse(html)

HERE,

  • from django.http import HttpResponse imports the HttpResponse object
  • def home_page(request): defines a function called home_page that accepts a parameter request.
  • html = "..." defines a variable html that contains some HTML code
  • return HttpResponse(html) returns an HttpResponse object that is initialized with the value of the variable html.

That is it for our view function. Next we need to update the file urls.py so that we can map the home page to our view function home_page

Open the file urls.py and import the function

from bookstore.views import home_page

HERE,

  • from bookstore.views import home_page imports the home_page function that is in the package bookstore in the module views.

Update the urlpatterns variable as follows

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

HERE,

  • path('', home_page), maps the empty path '' which is the home page to the view function home_page.

You can now start the server by running the following command

python manage.py runserver

Load the following URL into your web browser

http://localhost:8000

You should be able to see the following image

Django Urls

Multiple applications URLs Best Practices

In the above example, we used the project urls.py files and created a views.py file in it. We can define routes in urls.py directly but since we are separating our application logic using apps, it is better to create urls.py files directly into the respective app and define everything in there. We will then just link the urls.py file from the apps into the root urls.py file.

Create the following file urls.py in the directory /bookstore/books

Add the following code

from django.urls import path
from books.views import home_page

urlpatterns = [
    path('books', home_page),
]

HERE,

  • from django.urls import path imports the path function from django.urls
  • from books.views import home_page imports the home_page function from the views module that is in the books package.
  • urlpatterns = [...] maps the URL path books to the function home_page.

Save the changes.

Next, we need to define the home_page function in the file /bookstore/books/views.py

Add the following code

from django.http import HttpResponse

def home_page(request):
    html = "<html><body><h1>Books page</h1></body></html>"
    return HttpResponse(html)

HERE,

  • The above code imports the HttpResponse object and defines a function home_page that returns the value of the variable html.

The final step is to include the books/urls.py file in the root bookstore/urls.py file like so

from django.urls import path, include

HERE,

  • Imports the include path from the urls module that is in the package django

Next we update the urlpatterns variable to the following

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

HERE,

  • path('', include('books.urls')), maps the empty path '' to the file urls that is in the books package. This will make all the paths defined in books/urls.py available to the root urls.py. This way, we keep the root urls.py file clean and simple and include all detailed information in the app urls.py. Pretty cool huh!

Run the server by executing the following command

python manage.py runserver

Load the following URL into your web browser

http://localhost:8000/books

You should be able to see the following page

Django App URLs

Lesson Challenge

The only way to learn is by doing so we have designed this section to challenge yourself and apply what you have learnt so far.

We have the home page for the books app but we didn't do that for authors and categories app. Following the above example on books app, write the code for urls.py and views.py files for authors and categories then link them up in the root urls.py file so that the following URLs become part of our application.

http://localhost:8000/authors
http://localhost:8000/categories

By the time that you are done with this challenge, you should be able to see the following pages

Django App URLs

The categories page should be as follows

Django App URLs

Let us know in the comments section if you managed to solve the lesson challenge.

Summary

Virtual environments allow us to contain the application dependencies such as the python version, Django, pip etc. Separating your project into manageable apps is considered a good programming practice in Django and should be used when necessary. Each app should maintain its own URLs in the app directory as opposed to doing everything in the root projects. Apps are created using the django-admin utility and they should be registered in the settings.py file that is in the root directory.

What's Next?

In the next lesson, we will create the models and database migration files for our application.

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

Django Hello World

Django Models

Leave A Reply