Advertisement



< Prev
Next >



First Django Project



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






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/
	manage.py
	django_proj1/
		__init__.py
		settings.py
		urls.py
		wsgi.py







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 -

welcome/
	__init__.py
	admin.py
	apps.py
	models.py
	tests.py
	views.py
	migrations/
		__init__.py


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 :



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 -



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 -




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.




Please share this article -




< Prev
Next >
< Install and download Django
Django Template System >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement