DEV Community

balt1794
balt1794

Posted on

Chapter 2: Django Basics - Django 3...2...1...Takeoff! Series

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.

Models

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.

Screen Shot 2020-09-11 at 11.35.55 AM

Templates

Templates provide an interface between users and the website. The templates in Django are HTML templates. The templates don’t have to be completely static; you can also add JavaScript to make the frontend more dynamic, or even use a totally different frontend such as ReactJS instead of the templates provided by Django.

Views

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.

URLs

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.

Screen Shot 2020-09-11 at 11.37.20 AM

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.

Picture1

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.

i

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.

Listings Model

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.

Screen Shot 2020-09-11 at 11.40.06 AM

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.

Screen Shot 2020-09-11 at 11.40.52 AM

CharField

Used for small to large-sized strings.

ImageField

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.

DateTimeField

This class is used to display date and time.

FloatField

Used for floating-point numbers.

TextChoices

This is a subclass of the Choices class which allows us to display string choices.

blank

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.

str

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.

Screen Shot 2020-09-11 at 11.50.01 AM

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.

admin.site.register(model_name)

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.

Screen Shot 2020-09-11 at 11.50.56 AM

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.

Picture1

If you're enjoying the series and want to support, you can find the entire book below.

Django 3…2…1…Takeoff! Kindle

Django 3…2…1…Takeoff! Paperback

Top comments (0)