DEV Community

Cover image for Django Tutorial #4: Admin Panel
Eric Hu
Eric Hu

Posted on • Updated on • Originally published at techjblog.com

Django Tutorial #4: Admin Panel

You can download the source code of this tutorial here.
https://www.techjblog.com/index.php/django-tutorial-for-beginners/

The admin panel is one major advantage of the Django framework. Not only it is built into the framework, but it is also directly connected to the models. Whenever you make a change to the models, the corresponding admin page will be changed, no extra code required.

For this part of the tutorial, we’ll make one model and one corresponding admin page, just to see how things work. In future articles, we are going to talk about model relations and inline model admin, which are a bit more confusing for beginners.

Create A Model

Here we will create a model named Test, and it contains several different types of fields.

from django.db import models


# Create your models here.
class Test(models.Model):
    text = models.CharField(max_length=100)
    text_area = models.TextField()
    integer = models.IntegerField()
    date = models.DateField()
    boolean = models.BooleanField()
    file = models.FileField(upload_to='files/')
Enter fullscreen mode Exit fullscreen mode

Run the following command to generate the migration files. Remember you need to register the blog app like we talked about in the first part of this tutorial, or this command would not do anything.

python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

After that, apply the migration files with the next command. If this is your first time running this, the built-in migration files will also be applied.

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Create An Admin User

Next, we need an admin user who has the permission to log into the admin panel. Run the following command in the terminal:

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

img

Start the server and go to http://127.0.0.1:8000/admin/:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

img

img

Register the Admin Panel

Notice that the model we just created is not here, that is because we need to register it with Django admin.

blog/admin.py
from django.contrib import admin
from .models import Test

# Register your models here.
admin.site.register(Test)
Enter fullscreen mode Exit fullscreen mode

Refresh the admin page,

img

Now, try adding some entries to the Test.

img

Fix the List Page

When you save the entry, we will see something is wrong with the list page. Instead of the entry name, you will see only something like “Test object (1)”. That is not what we want. To fix that problem, we go to the Test model.

class Test(models.Model):
    text = models.CharField(max_length=100)
    text_area = models.TextField()
    integer = models.IntegerField()
    date = models.DateField()
    boolean = models.BooleanField()
    file = models.FileField(upload_to='files/')

    def __str__(self):
        return self.text
Enter fullscreen mode Exit fullscreen mode

Refresh the list page:

img

Play with Different Options

The Django admin is highly customizable. Let’s go back to the admin.py, and try some different options.

from django.contrib import admin
from .models import Test


# Register your models here.
class TestAdmin(admin.ModelAdmin):
    pass


admin.site.register(Test, TestAdmin)
Enter fullscreen mode Exit fullscreen mode

All the options for the Test model admin can be specified in the TestAdmin class.

date_hierarchy

Set date_hierarchy to the name of a DateField or DateTimeField in your model, and the change list page will include a date-based drilldown navigation by that field.

date_hierarchy = 'date'
Enter fullscreen mode Exit fullscreen mode

img

list_display

This option specifies what column will be shown in the list page.

list_display = ('text', 'integer', 'date', 'boolean')
Enter fullscreen mode Exit fullscreen mode

img

list_editable

Sometimes, it is very helpful to set certain columns to be editable directly in the list page.

list_editable = ('integer', 'boolean')
Enter fullscreen mode Exit fullscreen mode

img

prepopulated_fields

This option can be used to generate text from one column, and then populate another column with it. I always use it to automatically generate slug based on the post title like this:

prepopulated_fields = {"slug": ("title",)}
Enter fullscreen mode Exit fullscreen mode

Of course, these are only four options that I use the most. Here is a list of all ModelAdmin option that you can use.
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#modeladmin-options

Discussion (0)