Django Blog #5: Creating and Activating an Application

by Alex
Django Blog #5: Creating and Activating an Application
In the manuals for this framework you will come across the words “project” and “application”. In Django, a project is a Django installation with certain settings. An application is a group of models, views, templates, and URLs. An application interacts with the framework to provide various features. It can also be used in other developments. A project can be considered a website which includes applications such as a blog, a guidebook, or a forum which can be used in other projects.

Creating the application

Let’s create a blog application in Django. At the command line from the root directory of the project enter the following:

python manage.py startapp blog

This will create a standard structure:

blog/ 
    __init__.py 
    admin.py 
    apps.py 
    migrations/ 
        __init__.py 
    models.py 
    tests.py 
    views.py

The files in it perform these tasks:

  • admin.py – this is where the models to be used in the Django administration site are registered (you don’t have to use it).
  • apps.py – this directory includes the blog configuration.
  • migrations – this directory includes application database migrations. Django uses them to save information about model changes and synchronize the database accordingly.
  • models.py – application data models. All Django applications should have a models.py file, but it can remain empty.
  • tests.py – here you can add tests for the application.
  • views.py – this is where the application logic is located. Each view receives an HTTP request, processes it, and returns a response.

Application Activation

In order for Django to be able to track the application and be able to create databases for the models, the application needs to be activated. This requires editing the settings.py file and adding the blog.apps.BlogConfig item to INSTALLED_APP. It should now look like this:

INSTALLED_APPS = [
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
'django.contrib.sessions',
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'blog.apps.BlogConfig',
]

The BlogConfig class is the configuration of an entire application. Django now knows that it is active and will be able to load its models.

Related Posts

LEAVE A COMMENT