In this article, we are going to teach you how to run a simple Django Framework project.
Before we execute a Django project, we need to create a directory for our project, which contains all the required files for its proper execution. Let's see how to do that.
Creating a Django project
To create a directory for our project, we need to open the Command Prompt and type in the command -
django-admin startproject django_proj1
dhago-admin is a management utility for Django Framework, using which we could create a new project and much more.
startproject is a command to start a new Django project.
django_proj1 is the name of our Django project and also the name of our project directory created in the current directory.
The startproject command gives us a sub-directory and files
Executing the startproject command has not only created a project directory named django_proj1, but has also created a file manage.py and another sub-directory
with the same name as the project directory, which also contains some Python files -
django_proj1 is the root directory of our Django project, also known as the container of our project.
manage.py is a command-line utility which allows us to interact with Django project in various ways.
django_proj1 sub-directory is actually a Python package for our project. We can import this package(a group of modules) and its contents just like we import any general Python package/module and its contents.
__init__.py is a file which allows our sub-directory django_proj1 to be treated as a Python package.
settings.py is a file used for setting the configurations for our Django project.
urls.py is a file used to specify the URL declarations for our Django project, also known as a a table of contents.
wsgi.py is a file used as an entry-point for WSGI web-servers to serve your project.
Starting the development server
After creating the project directory and the files required to run our Django project, it is time to execute the Django Development Server. To do this,
we will have to open the command prompt, change the current directory to the root directory of our Django project and execute the following command.
python manage.py runserver
Executing the above mentioned command will trigger the Django Development Server in action, as you can see in the picture below.
Note: Please ignore the warning of unapplied database migrations for now, we will talk more about it later in the section - Database with Django.
Now that you have successfully started the Django development server, it is time to visit the http://127.0.0.1:8000/ link and you will see a webpage displaying a congratulatory message, as shown below.
Note: The runserver command runs the Django development server at the port 8000.
Creating an app in our project
A project may contain one or multiple applications. These apps are reusable i.e. they are portable and can be used in multiple project.
If you are building a simple application, you may only need a single app or no app at all, but for a complex application with multiple features, it's better to create multiple apps in it, where each app is doing a specific job.
As we have already created a project, now let us create a welcome application in it which welcomes the user. This application will be created right under our root - project directory django_proj1.
To create an application, we have to open the command prompt, change the current directory to the root directory of our Django project and execute the following command -
python manage.py startapp welcome
The command startapp in combination with manage.py utility is used to create an app of a project, using which we have created an app named - welcome.
Executing the startapp command has not only created an application directory named welcome in the root - project directory, but has also created the following set of Python files -
Next, we are going to create a view function for this app.
Creating a view function for the app
Once we have created an application, we will also need to create a view function associated with it. A view function is simply a Python function which takes an HttpRequest and returns an HttpResponse. The response could be an HTML page, an image, an XML document or even an error page.
Note: This view function, view.py is already created and stored in our application directory, welcome. Let us just copy and paste the following code in it.
view.py
from django.http import HttpResponse
def greet(request):
html = "<html><body>Welcome to the tutorial of Django Framework made by DecodeJava. </body></html>"
return HttpResponse(html)
Understanding the code in view.py function :
First, we have imported the HttpResponse from the django.http module.
Next, we have defined a view function named - greet, which takes an HttpRequest object in its parameter and we have named this parameter - request.
Note:Each view function takes an HttpRequest object in its first parameter and it could named anything but its typically named - request.
Next, in the defined function we have constructed an HTML response and have assigned it to a variable - html.
Finally, we have returned an HttpResponse object which contains the HTML response represented by the variable, html.
Mapping URL to view
After creating the view function, we need to specify the mapping between a URL and this view function in a file named - urls.py. The file urls.py is simply coded in Python and we will have to create the urls.py file in the folder of our application, welcome.
The use of mapping a URL to the view function is, when this URL is requested by the user, its mapped view function is executed and returns the response.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.greet),
]
As you can see in the file urls.py, we have imported the path() function from the module django.urls.
The path() function has been passed the two must arguments -
An empty string to specify the URL pattern, specified by ' '.
This URL pattern will be matched to the view function - greet, therefore it is specified by views.greet.
Pointing the project url to application url
Next, we are going to point the project url(urls.py) to the application url(urls.py) by using the include() function. For this, we will open the file urls.py in the root - project directory, django_proj1, and copy-paste the following code.
urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('django/', include('welcome.urls')),
]
As you can see in the file urls.py, we have imported the path() and include() function from the module django.urls.
The path() function has been passed the two must arguments -
A string to specify the URL pattern - 'django/'.
This URL pattern will be matched to the view function - greet, which is specified in the application urls.py file( stored in the application directory welcome). Therefore we have used the include() function to include and refer to the application's urls.py file.
Executing the application of our project
To execute the welcome application of our project django_proj1, we just have to enter the address - http://127.0.0.1:8000/django/.
As you can see in the picture, the string django in the URL is the URL-pattern which gets matched to the URL pattern specified in the project urls.py file and its associated view function - greet(in the application) is executed and its response is displayed.