Listings App
Learn how to create a simple Django web app.
We'll cover the following
Create project
In order to create a Django web app, we need to implement a few things. First of all, we need to issue the following commands in our desired directory.
django-admin startproject example .
python manage.py startapp listings
The first command creates a folder with the main Python files needed for our project. The dot (.
) at the end of the command is easy to miss but is important. The dot (.
) creates the project folder inside of our current working directory.
Create app
The second command creates an app that contains models.py and other necessary Python files. We can think of apps like small moving parts that work together to operate an entire machine, in this case, a website. In this project, we will use two apps:
- For users who are registered on our website.
- For listings posted by these users.
The directories and the files created by the startproject
and startapp
commands can be viewed in the widget at the end of this lesson.
Register app in settings.py
Every time we create a new app or install packages treated like apps, we ’ll need to register them in the INSTALLED_APPS
section in settings.py. Examples are django-rest-framework and django-allauth.
To register the Listings app, we need to make the following changes in settings.py in our project’s folder.
...INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','listings'] ...
If you have installed an app or package and you’re not sure whether it belongs to the installed apps section, check the online documentation for that package.
Live demo
Perform the following steps to run our first Django web app:
-
You should see the settings.py file in the widget below. Refer to the code snippet above and add
'listings'
in theINSTALLED_APPS
field in settings.py. -
Run the app by clicking the Run button.
-
You should see an output similar to the one demonstrated in the image below.
""" ASGI config for example project. It exposes the ASGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ """ import os from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings') application = get_asgi_application()