Django project structure

Django separates different functionality in different files. The folder structure of a newly created project looks like as follows:

Let’s explain some of these file and folders:

Once you create a portfolio_blog project, it contains another folder with the same name portfolio_blog. This folder is the main app it contains various files. 

Inside folder portfolio_blog/ 

  1. __init__.py -> This file is there to represent it as a python package, as you already know any folder with an __init__.py file is considered a package.
  2.  asgi.py: ASGI (Asynchronous Server Gateway Interface) is a spiritual successor to WSGI, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications. This file is used while deploying python and handles asynchronous connections.
  3.   settings.py: This file contains our application settings, things like the connection with the database, which apps are installed/used, settings for the media file and static files, settings for templates folder, etc are contained in this folder. This file is one of the most important files in the project as it governs all our settings to our application. 
  4.  urls.py: This file contains our URLs for our application. This file can also point to another urls.py file. The URLs are a way of connecting the URL path to a specific view function or class. For instance, when you type http://localhost:8000/about, these links are mapped in urls.py and the urls.py file decides what to render when this link is called.
  5. wsgi.py -> This file stands for web server gateway interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. WSGI is a Python standard described in detail in PEP 3333.

Now, let’s see some files outside main application folder i.e. portfolio_blog 

db.sqlite3: This is the SQLite database. It contains all your data, and by default, Django uses SQLite database. If you wish to use another database such as MySQL, Postgres, or MSSql, you can configure it in the settings.py file. 

manage.py: This is the file that lets us run commands. If you want to run any command that involves the manage.py file you first need to be in the same directory as manage.py. 

Some useful commands are:

  • python manage.py runserver -> this command is used to start the Django application
  • python manage.py makemigrations -> when ever you add something to model this command create a model file to perform operations in a database.
  • python manage.py migrate -> this file executes command created by makemigrations file and commits to a database. 
  •  python manage.py collectstatic -> This command is used to aggregate static files. 

By default python manage.py runserver , runs the server in 8000 port if you want to run it in a different port you can provide port as python manage.py runserver 8080

Also, Django separates application functionality in various apps. You can easily use one app created in Django in another Django project. In the next post, you will learn how to create an app in Django and understand its folder structure. 

Leave a Reply

Your email address will not be published. Required fields are marked *