In this topic, we will learn step by step process to create a Django application.
To create a Django project, we uses the following command:
$ django-admin startproject projectname
( projectname is the name of Django application)
Here, we are trying to create a Django project djangoapp in the current directory.
$ django-admin startproject djangoapp
Now, we will move to the project by changing the directory.
The Directory can be changed by using the following command:
To see all the files and the subfolders of the Django project we can use tree command that will show us the tree structure of the application.
The outer directory folder is just a container for the application. We can also rename it further. The Django project itself contains the following packages and files.
Django project has a built-in development server which is used to run application instantly with none external web server. It means we do not need Apache or another webserver to run the application in development mode.
To run the application, we can use the following command.
$ python3 manage.py runserver
Create an Application
In our main “myproject” folder, the same folder then manage.py −
$ python manage.py startapp myapp
We have just created “myapp” application and like the project, Django creates a “myapp” folder with the application structure −
myapp/ __init__.py admin.py models.py tests.py views.py
Now, we have our "myapp" application, we need to register it with our Django project "myproject". For this we have to update INSTALLED_APPS tuple in the settings.py file of our project −
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', )