Django MVC Pattern
Django is a web framework which is also known as a Model-View-Controller (MVC) framework. Django follows the MVC, but there is a slight change when it comes to these concepts. The three most important parts to understand are the following.
Django uses Object-Relational Mapping (ORM). This simplifies the interaction to the database since we don’t have to write complex SQL to create tables and other stuff in the database. Models contain the fields and types of data to be stored in your database.
Django’s views pass the data from the database to the templates, so that it can be shown to users in a more elegant and organized way, and not just as a bunch of tables.
There are two types of views; function-based views (used in this book) and class-based views.
To navigate around websites, Django uses URLs just like any other website. When a user wants to go to a page and clicks on a link, Django receives a URL request from the user and looks in urls.py to see to which view the URL being request is tied to. After Django calls the right view, this view renders the content to a template which is what user will see.
Django Admin Site
Django provides an admin panel which allows us to manage the content of our site. To access the admin panel, we need to create admin credentials first. Only people that you trust to manage the content of your site should have access to the admin site.
Let’s put in practice all the theory by creating the listings model and going through the process of using the admin panel to create and manage some listings. We won’t be using templates since the Django admin site already provides with one for us.
Create admin credentials by issuing the following command. You can use the credentials below, but feel free to enter your own as you go through the process.
Issue the command python manage.py runserver and go to http://127.0.0.1:8000/admin/ to see the login page for the Django admin site.
Enter your credentials and log in. The admin site appears almost empty as of now since we haven’t created any models yet, but you should see two fields which correspond to the Authentication and Authorization section which is provided by Django.
Django provides us with an authentication system that manages user accounts, groups, permissions, and more. Later on, we will use the Django authentication system to register and create users. For now, let’s click on users to see the superuser that we created from the terminal.
You should see the username and email address we entered when creating the superuser. All the way to the right, you will see the staff status which shows a green check which means that this user can access the admin site since it’s part of the staff.
The first and last name fields are empty, but if you select the user, you can add information, add the user to specific groups, edit fields, and even delete the user. Explore the admin panel before moving to the next section.
Since we already created the listings app and added it to settings.py, we can proceed to create the listings model in models.py.
Open models.py and start adding the following imports.
from django.db import models
This import allows us to create models. Every model that we create will inherit from django.db.models.Model, in other words, each model is a subclass of this import.
from django.utils.timezone import now
This import gets the current time for each user based on their individual time zones. If users live in different time zones, this import will adjust that accordingly.
from datetime import datetime
Allows us to get the date and time by providing attributes such as year, month, day, hour, minute, second, and more.
The listings model will have different attributes also known as fields which users will be able to enter or select in order to create a listing. There are many fields that you can add to your listings model depending on your project requirements, so don’t feel obligated to include all the fields listed here. Feel free to edit and add your own fields as needed.
After we’ve added these imports, we can add the following code which will create the listings model.
Used for small to large-sized strings.
This class has the same attributes and methods from FileField, but also checks if the uploaded file is a valid image. The FileField class can also be used to upload images.
This class is used to display date and time.
Used for floating-point numbers.
This is a subclass of the Choices class which allows us to display string choices.
By default, blank is set to False which means that the field will appear as mandatory. This means that the field needs to be filled out otherwise the form won’t be submitted.
When blank is set to True, users can leave the field empty and the form will still be submitted successfully.
This method allows us to convert an object into a string. This string representation will show in the admin panel with the field of the object that we return (in this case, the title field).
If we don’t call this method, after we migrate the changes, the admin panel will return the object, but it wouldn’t show a clear name in the admin panel hence difficult to identify.
Before we make migrations to the database, let’s register the model in admin.py, so that it appears in our admin panel.
Go to admin.py in your app’s folder and make the following changes.
from django.contrib import admin
Imports the admin module.
from .models import model_name
Imports the model into admin.py, so that we can use it without having to write it again. The dot tells Django to search for the model in models.py of the same directory as admin.py.
Registers the model in the admin site.
After we’ve made the respective changes to models.py, issue the migration commands and run the server.
After you run the server, access the admin site again. You should see a new option for Listings below Groups and Users. Select Listings > Add Listings.
Create some listings of your own. After you do, the listings page should look like the screenshot below. You can see and edit specific listings by clicking on them. Feel free to add more.
If you're enjoying the series and want to support, you can find the entire book below.