App Structures
App Structures
A Django project is a web application that may consist of one or more sub-modules called apps.
In this reading, you'll learn about the app structure, how to create an app and how to configure
project settings to include the app.
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.
For simplicity, you are going to uniformly use one app inside one project throughout the course,
but it is good to know the utility of Django framework as a multi-app framework.
When a Django project is created with the startproject command, it creates a container folder.
Django puts a manage.py script and the project package folder in the outer folder.
The startapp command option of the manage.py script creates a default folder structure for the
app of that name.
C:\djenv\demoproject
│ db.sqlite3
│ manage.py
│
├───demoapp
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ └───migrations
│ __init__.py
│
└───demoproject
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
views.py
In Django, a view is a user-defined function that’s called when Django’s URL
dispatcher identifies the client’s request URL and matches it with a URL pattern
defined in the urls.py file.
Let's add a view function called index() in it by saving the following snippet
On similar lines, you need to provide the URL routing mechanism for the app.
One important thing to note here in contrast to the video you have seen earlier
is that 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.
The app folder doesn’t have a file of this name when created. Hence, you have to
create one.
urlpatterns = [
path('', views.index, name='index'),
]
Next, you need to update the urlpatterns list in the project folder’s urls.py and
include the app’s URL configurations.
urlpatterns = [
path('demo/', include('demoapp.urls')),
path('admin/', admin.site.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
Lastly, you 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:
NSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'demoapp',
]
After Performing the steps explained above, run the Django development server and visit
localhost:8000/demo/ in the browser.