Django Tutorial #4: Admin Panel

    Django Tutorial #4: Admin Panel

    More Tutorials on Web Development


    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 the 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/')

    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

    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

    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

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

    python manage.py runserver

    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)

    Refresh the admin page,

    Now, try adding some entries to the Test.

    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

    Refresh the list page:

    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)

    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'

    list_display

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

    list_display = ('text', 'integer', 'date', 'boolean')

    list_editable

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

    list_editable = ('integer', 'boolean')

    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",)}

    Of course, these are only four options that I use the most. Here is a list of all ModelAdmin option that you can use.

    Leave a Reply

    Your email address will not be published. Required fields are marked *