Introduction
Django is a Python based Framework which allows Full Stack Website Development. It’s not one of the easy Frameworks that Python has to offer, so actually building an Application would be the best way to learn!
In this blog you will learn about the structure and the use of files in Django.
Setup
- Python (3.x):
sudo apt-get install python3
- Django (2.0):
pip install django==2.0
- An IDE (PyCharm etc) or a Text Editor (VS Code, Sublime Text etc (In order of personal preference)).
Getting Started
We will first create a Django Project. To do so, type in django-admin startproject myfirstdjangoproject
. If you move into the newly created directory, you will see a directory structure similar to this:
- myfirstdjangoproject
- __init__.py
- settings.py
- urls.py
- wsgi.py
- manage.py
Let’s now go over the important files in the directory.
settings.py
: This is the file which contain all of the configurations and settings of the complete application. All of the apps (which we will discuss a little later), the Database Configurations etc, will be mentioned in this file.
urls.py
: This file contains all the possible urls that a user accessing the Website can visit. Also, each url mentioned will be connected to a view (this will also be explained in detail a little later).
manage.py
: This file configures the project. It will be used later to Sync our Database with all of our Data Models and also most importantly, to run the local server!
Applications
According to the Django Documentation, an Application is basically a Python package that provides some set of features. You can read more about Applications here.
We will now create our first app. Type in python manage.py startapp firstapp
. Now, the directory structure will look something like this:
- myfirstdjangoproject
- __init__.py
- settings.py
- urls.py
- wsgi.py
- firstapp
- migrations
- __init__.py
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- manage.py
admin.py
: This file is used to connect the Data Models with the Admin Interface (that is inbuilt in Django!)
models.py
: This file will contain all of the Data Models that will be required in the Application. An example Item Data Model is given below:
class Item(models.Model):
title = models.CharField(max_length=100)
unit_price = models.DecimalField(max_digits=10)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField()
description = models.TextField()
image = models.FileField()
views.py
: This file is basically responsible for what happens when the user accesses a URL. This is the file that will contain all the functionality required by a Project. Each view is connected to a URL, so whenever a user accesses a URL, the corresponding view is called. An example View is given below:
def index(request):
items = Item.objects.all()
return render(request, 'main/index.html', {"items": items})
In a project, multiple Applications can be created, and each of the Applications will have it’s own set of views.py, models.py and admin.py
.
If you have any further queries relating to the functions of each of the files, the Django Documentation is the best place to clear them out!