Creating a Django application

Once we have the base and file structure, we can create our application. To create an application, we need to execute the following command in the console:

$ python manage.py startapp djangoApp

With this command, a new djangoApp directory has been created with the following structure:

After creating an application, we also need to tell Django to use it. This is done in the djangoApplication/settings.py file, where we have to find the INSTALLED_APPS array in the Application definition section and add a line that contains the name of our  djangoApp application:

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djangoApp'
]

We can create our model in the djangoApp/models.py file and define the object Model inside this file that represents a post from a blog. This model includes information about the author, title, content, and timestamp:

from django.db import models

class post(models.Model):
author = models.CharField(max_length = 30)
title = models.CharField(max_length = 100)
content= models.TextField()
timestamp = models.DateTimeField()

The last step is to add our new model to the database. First, we have to let Django know that we have generated a new model with the following command:

$ python manage.py makemigrations djangoApp.

This is the output we receive when we execute the previous command:

Migrations for 'djangoApp':
djangoAppmigrations001_initial.py
- Create model post

In this way, Django has prepared a migration file that we have to apply to our database with the following command:

$ python manage.py migrate djangoApp
Operations to perform:
Apply all migrations: djangoApp
Running migrations:
Applying djangoApp.0001_initial... OK

Once we have defined our models, we can use Django to manage the objects of our model. We can do this by using the administrator (admin) of Django. To do this, you must go to the djangoApp/admin.py file and add the following code:

from django.contrib import admin
from .models import post
admin.site.register(post)

We can use the admin.site.register (post) command to register our model in the application. This can also be used in the Django administrator page.

With this, we are able to execute the python manage.py runserver command to run the web server and access http://127.0.0.1:8000/admin . You'll see a sign-in page:

To log in, first, you must create a user in the Django database, which is a user that has control over the entire site. By executing the $ python manage.py createsuperuser command, we can create a user to access the administration area:

Username: admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully

After logging in with the user that we just created, we can see the Django administration panel:

From here, we could, for example, create a post for our application with the Add button:

We can also see the objects that are saved in the Django database:

For more information, check out the official Django documentation at https://docs.djangoproject.com/en/2.0/ref/contrib/admin.

In this section, you have learned about the MVC paradigm in the construction of dynamic web applications, and were introduced to the Django framework, its main commands, and the administration panel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset