In this Python tutorial, we will understand How to setup Django project. For this, first, we will learn How to create a Django project, How to create a Django app, and How to run your first Django project. In this tutorial, we will cover the topics in the following sequence.
- How to create Django project
- How to run Django project
- How to stop Django project
- How to create Django app
- How to setup Django app
How to setup Django project
Before moving forward, first, we should install python and Django in our system. And for this, you can refer to the following article “How to install Django“.
Now, the steps for setting up your first Django project will be the same for all the operating systems. We can use different tools or utilities like visual studio code, pycharm, command prompt, etc. But the commands and steps for them will also be pretty much the same.
For this tutorial, we will be using the command prompt in Windows 10 for demonstration.
After installing Django, before setting up your first project in Django, we need to understand How we can create a project in Django.
How to create Django project
In this section, we will understand How we can create our first Django project. But let’s first understand what is a project in Django.
A project in Django can be referred to as a whole web application with its all parts. It can also be referred to as a collection of settings and apps for a specific web application. A Django project can contain multiple apps which have some different functionalities. And we can also use these apps in different projects.
Let’s understand this with a real-life example, and the example is as follows.
I want to build an e-shop using Django. So, the e-shop will be my Django project. Now, the project will have some functionality like register new users, add new products to the e-shop, etc. For this, we will create Django apps within our e-shop project.
Now, let’s see how to create our first Django project. For this, open the command prompt and move to the location where you want to create your project. And now, run the following command.
django-admin startproject project_name
Let’s use this command to create our first Django project.
This will create a new directory with the name myProject in our current location. And this new directory will also contain some important python files which have their own importance. Here is the directory structure.
Here, myProject/ is a root directory that contains manage.py file and another directory with the same name myProject.
Now, the name of this root directory is not much important, we can also change this name. But the other directory with the name myProject within the root directory is very important as it is a python package. And we have to use this name to import anything inside it.
Now let’s understand the importance of these automatically created files in the project directory.
- manage.py– It is a command-line utility that is used to interact with your Django project.
- __init__.py– It is an empty file that indicates python to consider this directory as a python package.
- settings.py– This file contains all the settings and configuration of your Django project.
- urls.py– This file is used to create and manage URLs related to your web application.
- asgi.py– It is used for ASGI configuration for your Django project.
- wsgi.py– It is used for WSGI configuration for your Django project.
So, with this, we have successfully created our first project in Django.
How to run Django project
After successfully creating the project now it’s time to understand how we can run the Django project.
When we install Django, it comes configured with a lightweight server. And if we want to develop a web application locally in our system then, this lightweight server is efficient for this task.
Note- It is not recommended to use this lightweight server in production. So, always configure the Django project with the server that you want to use in production.
Now to run our project, we need to start the server, for this, we will use the manage.py utility. So, move to the project root directory and use the following command.
python manage.py runserver
This will return an output similar to this.
As you can see in the output, there are no issues with our project and the development server is been started. Now, we can access our web application at the following URL “http://127.0.0.1:8000/“. And when we open this URL in our browser, it will return a congratulation message. Here is how it will look.
So, with this, we have successfully started our first Django project.
How to stop Django project
Once we have started our project, the development server will be automatically reloaded when we make some requests. It will also reload when we make some changes in our python code. There are very few situations when we need to restart the development server.
Now, to restart our development server, we should know, How to stop a running development server. For this, we just need to enter “Ctrl+C” and it will stop the server immediately.
Read: Python Django vs Flask
How to create Django app
Till now, we have learned How we can create a project in Django. Next, let’s understand How to create an app for our Django project. But for this, let’s first understand what is an app in Django.
An application in Django is a submodule of a Django project. It is also a python package that is used to provide some set of features. It is a standalone python package with its own set of files like models, views, templates, template tags, static files, URLs, etc.
In Django, we can easily create an app by using the manage.py utility, and it will automatically all the files required in an app. So, we don’t need to create an application directory from scratch. Here is the simple command to create an app in Django.
python manage.py startapp app_name
Let’s use this command to create a sample app for our Django project. For this, first, move to the same directory where the manage.py file exists, and execute the following command.
python manage.py startapp sampleApp
This will automatically create a new directory with the name sampleApp in the root directory. Now, this sampleApp directory will contain some python files. So, its directory structure will be as follows.
Now, an application directory in Django also contains some set of python files. Let’s, understand their useability.
- admin.py– It file is used to display and manage your application model data at the admin panel side.
- apps.py– It is used for appliaction configuration.
- models.py- This file is used to define python objects referred to as Models.
- tests.py- This file is used to define units tests for models and other components of your application.
- views.py- This file is used to define your business logic.
With this, we have created our first application for our Django project.
Read: What is Matplotlib
How to setup Django app
So, till this point, we have successfully created our first app in the Django project. Now, let’s add some functionality to our application.
For this, first, we will go to the sampleApp\views.py file and add the following code.
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome Everyone to PythonGuides")
In the above code, we created an index view that will simply return the given string as a response. Next, we need to map this view to a URL. For this, first, we need to create the urls.py file in our application directory. After this, we need to add the following code in the sampleApp/urls.py file.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
In the above code, we have created an empty string URL pattern for our index view.
Next, we need to add our sampleApp.urls configuration to the root urls configuration. For this, open myProject/urls.py and add the following code.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('page1/',include('sampleApp.urls'))
]
Now, simply run the development server and then open the following URL “http://127.0.0.1:8000/page1/“. And we will get the following output.
You may also like reading the following articles.
- Python Django get admin password
- Difference between app and project in Django
- Create model in Django
- Python Django get enum choices
So in this tutorial, we have learned How to setup Django project and we have also covered How to setup Django app. And we have also demonstrated the following topics in this tutorial.
- How to create Django project
- How to create Django app
- How to setup Django app
- How to run Django project
- How to stop Django project
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.