Django Web Framework[Part-3]

Debapriya Basu
3 min readAug 11, 2023

--

Now we will learn about Django admin and structures. Let’s grab some hot coffee ☕️ and take a sip and start 🍀!

App structures

A Django project is a web application that may consist of one or more sub-modules called apps.

What is a Django app?

An app is responsible for performing one single task out of the many involved in the complete web application, represented by the Django project.

It implies that a project comprises many independent sub-applications, yet they may communicate among themselves.

For example, a trading organization website may have one app for managing customer data, another for suppliers, and another for stock management. However, the important feature of the Django app is that it is reusable.

Hence, a customer data management app in one project can be linked to the website project for another organization without modification.

Django framework is a multi-app framework.

Create an App under main Django folder

(django-tutorial-env) dbasu@Debapriyas-MBP chefsTable % python3 ./manage.py startapp demoApp

The below App folder along with files is created now.

Let’s briefly explain the Python scripts created in the demoapp folder.

views.py

In Django view is a user defined function that is called when Django’ URL dispatcher identifies the client’s request URL and matches it with a URL pattern defined in urls.py file.

This file is empty while auto created.

We can write one index function here.

from django.http import HttpResponse

# Create your views here.
def index(request):
def index(request):
return HttpResponse("Hello world. This is index view of DemoAPP!")

urls.py

The project package has a file named urls.py which is used to define URL patterns of the project.

You need to provide the URL routing mechanism for the app.

The urls.py file can be configured at both the project and app level. In the example below, the urls.py will be configured at both the project and app-level.

In App folder we add this below urls.py file.

from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name = 'index'),
]

Next, we need to update the urlpatterns list in the project folder’s urls.py and include the app’s URL configurations.

from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
path('demo/', include('demoApp.urls')),
]

models.py

The data models required for processing in this app are created in this file. It is empty by default. A data model is a Python class based on django.db.modelsclass.All the models present here are migrated to the database tables. For now, leave his file as it is without adding any models. You will learn how to work with models later.

tests.py

You’ll write the tests to be run on the app in this file. Let’s keep this file as it is without modifying it.

Update settings.py

We need to update the list of INSTALLED_APPS in the project’s setting file. This list already contains some pre-installed apps. Add the name of demoapp so that it looks like this.

Updated settings.py with new app name

What’s Next

Previous topics ⭐️👇

🎯DJango Web Framework[Part-1]

🎯DJango Web Framework[Part-2]

Next topics ⭐️👇

🎯Django Web Framework[Part-4]

🎯Django Web Framework[Part-5]

🎯Django Web Framework[Part-6]

🎯Django Web Framework[Part-7]

I will sum up few more topics which I found interesting to look at. Let’s get learning🍀!

🙏 Thank you for reading to the end! Comment below or email me at dbpr.sinha@gmail.com if you have any questions. For more articles please stay tuned. Also follow my links.

--

--

Debapriya Basu

Engineer @ IKEA group with product mindset and ready to take any challenges.