In the first part of this Django tutorial blog, I had brushed up on what Django is and why developers prefer Django for web development. I had also covered how to install and create a virtual environment and install Django in it. Please refer to my previous blog post here to follow along.
In this blog post, we will cover how to start a Django project and the file structure of a basic Django project.
Creating a project
Creating a Django project is fairly straight forward. Django auto-created the file structure, including database configuration, Django-specific options and application-specific settings.
From the command line, cd
into a directory where you’d like to store your code, then run the following command:
django-admin startproject mysite
django-admin
is a command-line utility in Django for administrative tasks. This should create a basic project structure for a Django project in the current directory and the file structure must look like below. If you faced any issue executing the above command, then check here for advice about errors and problems commonly encountered during the development of Django applications.
mysite/
manage.py
*db.sqlite3*
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Let me brief you these files and directories that were auto-generated by Django.
Outer mysite - Container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
manage .py - This file is the command-line utility for your Django project. It lets Django developers deploy, debug and test Django projects in development as well as a production environment. This python file contains the code for starting the server, migrating and controlling the project through the command line.
db.sqlite3 - This file might not be present when you execute the
startproject
command but will be auto-generated when needed by Django if you're using the SQLite database (Will come back to this point on coming tutorials).mysite/init.py - An empty file is generated to tell Python that this is a Python package. All Python packages must contain this file to tell Python the entire directory is a package. To know more about Python modules and packages refers to this article.
settings.py - The setting file contains all the configurations for your Django project. The DJANGO_SETTINGS_MODULE environmental variable tells Django which settings you're using. By default this variable points to the seeting.py file. It can also be changed. This file contains the database settings, a list of all installed apps and middleware, timezone, language code etc. As we keep adding apps onto our Django project, we have to update the setting.py file to keep Django synchronised with the changes.
urls.py - This file is like the index page for your Django project. In any Django powered website, Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against requested URL. All such patterns are available at this urls.py file.
wsgi.py - WSGI stands for “Web Server Gateway Interface”. It is used to forward requests from a web server to a backend Python web application or framework. The wsgi.py file acts as an entry-point for WSGI-compatible web servers to serve your project.
asgi.py - ASGI stands for "Asynchronous Server Gateway Interface". It is a successor to WSGI. The asgi.py file acts as an entry-point for ASGI-compatible web servers to serve your project.
Running the Django server
As mentioned in the previous post, Django comes with a lightweight webserver which can be used for rapid development of Django projects without having the headache of configuring a production server until your site is ready for it.
We can verify is the Django project that we created is working by running the following command on the command prompt in the project directory
python manage.py runserver
If you don't encounter any errors on running this command, it means that your server is successfully running. You can visit http://127.0.0.1:8000/
with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!
By default, Django uses port 8000. It is possible to change the port along with the server's IP using the following command.
python manage.py runserver <ip-address> : <port-number>
Congratulations! You have successfully created your first Django project. In the next article, we will see how to create apps inside your Django project and link your app to the project.
Top comments (0)