In our previous blog Introduction to Django, we discussed the Django’s features and architecture. In this blog, we will create a web application in Django.
For starting a new project, go to the folder where you want your project to be and run the command:
django-admin startproject django_proj


django-admin
Django’s command-line utility for administrative tasks.manage.py is automatically created in each Django project. manage.py does the same thing as django-admin but takes care of a few things for you:
-
It puts your project’s package on sys.path.
-
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file. In our case, in manage.py file you can see the following:
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘django_proj.settings’)
-
Run
django-admin help
to display usage information and a list of the commands provided by each application. -
Run
django-admin version
to display the current Django version.
Apart from manage.py, you can see the django_proj folder, which is the name which we used for creating a project. Inside this folder, there is a file __init.py__ which is empty it just tells python that this is a python package.
setting.py
-
is used for all kinds of configurations for example database.
urls.py
-
is used for mapping URL to views. Right now, you will see :
urlpatterns = [
path(‘admin/’, admin.site.urls),
]
We will discuss it later when we run the application.
wsgi.py-
Django’s primary deployment platform is WSGI (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.
Django’s startproject
management command sets up a simple default WSGI configuration for you.
Run application
python3 manage.py runserver
It will start your application at http://127.0.0.1:8000/
You can see the unapplied migrations warning on the terminal:
To access your Admin Interface, you need to initiate the database.
Initiate Database
python3 manage.py migrate
It will create necessary tables or collections depending on your DB type(default is SQLite), necessary for the admin interface to run.
Create Superuser
python3 manage.py createsuperuser
Then start your application again, as we saw in urls.py, where admin path is stated.
Go to http://127.0.0.1:8000/admin
Thanks for reading!