In part 1 of this Django tutorial series: introduction to Django, we introduced you to Django, popular sites using Django and what can Django do. And in part 2, we showed you how to install Django in a virtual environment.
In this beginner Django tutorial series part 3, we will show you how to start a Django project, how to start a Django app and then go ahead to create our first website with Django (Hello, World! site). This is a Django tutorial series, so if you haven’t read part 2, then see Part 2: How to install Django in a virtual environment to get started.
Starting a Django Project
Now that we have installed Django in a virtual environment (in part 2), we will start a Django project and build our first website with Django.
Django comes with the concept of project. A Django project is the entire website you are creating. It contains several other project configuration files, folders and apps/modules.
To start a new Django project, run the command below:
django-admin startproject django_project
django_project is what we are naming our Django project.
After running the startproject command, it will generate a directory structure for your Django project.
Let’s look at the files created by the startproject command:
- __init__.py – This is an empty file that tells or instructs Python to see or treat this folder as a Python package.
- settings.py – This file contains all of the project or website settings and configurations, like: database, media & static file configurations, registration of an app, etc.
- urls.py – This file allows you map your URL to a view.
- wsgi.py – This file means Webserver Gateway Interface. It basically allows your Django project communicate with the server.
- asgi.py – Similar to WSGI. It means Asynchronous Server Gateway Interface. This file allows asynchronous communications between apps.
The manage.py file is a project management script. It is used to start the Django development server, start Django apps, etc.
For us to check if we successfully started a new Django project, we will have to change directory into the Django project directory. Now run the following command in your terminal:
django_project is the name of the project we created earlier.
After changing directory into the Django project you created earlier (django_project), let’s run the server. Run the command below in your terminal:
python manage.py runserver
Now copy out your development server URL, which is usually http://127.0.0.1:8000.
If you see a screen like the screenshot below, then the development server has been started, and you successfully started a Django project using the startproject command.
Starting a Django App
Django comes with the concept of projects as well as apps. Django apps are separate standalone or independent modules that make up a Django project. They are independent of other Django apps that are present in the project! A Django project consists of project configuration files as well as apps, while a Django app are modules that make up a Django project. We haven’t started a Django app for this project yet, let’s go ahead and start a Django app for our project.
Type ctrl + c to stop the development server.
Now to start a new Django app, type the following Django command into the terminal and allow a Django app to be started for you. Since we are creating a blog in this Django tutorial series, then we will name our Django app blog.
python manage.py startapp blog
Some files and folder were added after executing the startapp command. Let’s look at them:
- __init__.py – this is an empty file that tells or instructs Python to see or treat this folder as a Python package.
- models.py – the models.py file is responsible for defining entities, which are automatically turned to database tables for your Django apps.
- views.py – the views.py file is responsible for handling incoming requests from the user to the views, and responses from the views back to the user.
- apps.py –
- admin.py – this is a Django app configuration file that is specially created for the built-in Django administrator.
- tests.py – this file allows you write some unit tests for your Django apps.
- migrations/ – this folder keeps track of the changes made to your Django app models.py file, by creating a revision file after each change has been created. The files in this folder allows you to easily revert to older changes made in your models.py file.
Registering a Django App
Django emphasizes independent or standalone components which can be included or excluded from a Django project. And we must tell Django about each independent or standalone component that is being installed or included in a Django project. This means that for each Django app you start, you must register it. This tells Django that this app is to be considered part of the project! If you do not register your Django app, then Django will not know or recognize that the app is part of the project.
To register a Django app, open the settings.py file in your Django project root directory. ctrl + f and find the INSTALLED_APPS section. This is what it looks like:
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
By default Django comes with 6 apps preinstalled. And also allows us register as many apps as we may want.
Now add the blog app into the list of INSTALLED_APPS and save.
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', ]
That is how to register a Django app!
Note – to start a Django project use the django-admin startproject project_name command, while to start a Django app, use the python manage.py startapp app_name command. Replace project_name with your project name and app_name with your app name.
Creating a Django Site (Hello World Site)
In this section, we will write our first view which will show Hello, World! in the frontend, then map it to a URL.
Creating a Django View
Open the views.py file in your blog app and update it with the following lines of code:
from django.http import HttpResponse # homepage view def home(request): return HttpResponse('Hello, World!')
First we imported HttpResponse from django.http. Next we created a view named home. This home view will serve as our blog’s home or index page. A Django view takes the request object as the very first argument. So, we passed request to the home view.
After that, we indented our code and returned the HttpResponse we imported earlier, and passed Hello, World! to it.
Mapping a Django View to a URL
Here, we will map the view to a URL so that we can view it using the URL we mapped it to.
Open the urls.py file in your Django project directory and update it with the following lines of code:
from django.contrib import admin from django.urls import path from blog import views urlpatterns = [ path('', views.home, name='home'), path('admin/', admin.site.urls), ]
We imported the views file from the blog app. Notice we did not add .py to the views file while importing. That is how Python imports a file. You don’t add the .py extension to the file name even though it is a Python file.
After importing views from blog, we used the path function we imported from django.urls to map the views to a URL.
The first argument in the path function is the URL we will visit to see the response from the views. In this case, we have an empty quotation mark with nothing inside of it (”). This means that the URL mapped to this views will be accessed via http://127.0.0.1:8000. However, if we had used path(‘hello-world/’, views.home, name=’home’),, then it will be accessed via http://127.0.0.1:8000/hello-world.
After that, we assigned the views.home function we created to this particular URL. name=’home’ is the name of the URL. It is very important to name all of your Django URLs, as they will be used for reference purpose.
Save the urls.py file and visit your Django development server, usually http://127.0.0.1:8000.
If your Django development server is not started, use the command below to start your server:
python manage.py runserver
If you see something like the screenshot below, then congratulations on your Hello, World! site.
Django Tutorial Series
In this Django tutorial series, we have been able to cover part 1 – part 2. If you need to read them again, please see:
We hope this Django tutorial series – creating an Hello, World! Site (Part 3) helped you learn how to start a new Django project, how to start a new Django app and how to create an Hello, World! site with Django. This is a Django tutorial series, so if you haven’t read part 2, then see Part 2: how to install Django in a virtual environment to get started.
See other of our Django tutorials for more.