In the last section , We have created a Django Model Post, And make Post Model and database synchronization . In this section , We will introduce Django Manage site , adopt Django Manage the site to manage the Post Model examples .

Create a control management site for your model

Okay , We have defined a post Model , Now? , We're going to create a simple administration site , To manage post example .Django The management interface is a very powerful content editor .Django The management site is built dynamically according to the metadata of your model , And provide a convenient interface to edit content . Again , You can also customize how to present your modules in the management site .

One thing to remember is django.contrib.admin Already included in INSTALLED_APPS Setting up , That's why we no longer need to add it manually .

Create administrator privilege users

First , We need to create a user with administrator rights to log in to the management site , Run the following command :

python createsuperuser

You'll see the following , And prompt you to enter the user name 、e-mail, password :

Username (leave blank to use 'admin'): admin
Email address:
Password: ********
Password (again): ******** Superuser created successfully.

Django Manage site

Now? , Run the development server (python runserver), Open in browser, You can see the management site login interface .

Enter the administrator user name and password you just created , Log in to the administration site , You'll see the home page of the management site .

Now what you see User and Group The module is Django The authentication framework for , This is INSTALLED_APPS Medium django.contrib.auth Set up . If you turn it on Users, You'll see the administrator you just created .blog In application Post Models and User There are relationships between models . This association is defined in Post In the model author attribute .

Add your model to the administration site

Now we're going to create blog In application Post Model added to management site . edit blog In application file :

from django.contrib import admin
from .models import Post

Now? , Reload the administration site , You can see your Post The model has been added to the administration site .

This is easy. , Am I right? ? When you are in Django Manage site registration a model , You get a friendly user interface : Allows you to generate a list of objects ; And very easy editing 、 establish 、 Delete object .

Django Use different form plug-ins for different fields , image DateTimeField, Show a similar in the administration site JavaScript Calendar plug-in interface . After adding everything , Click the save button . The site will jump to post List of pp. , And give a ” You have successfully added post“ A hint of .

Customize the module display in the management site

Now we'll see how to customize the administration site . Edit your blog In application file :

from django.contrib import admin
from .models import Post class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status'), PostAdmin)

We tell Django Manage site , We use inheritance from ModelAdmin User defined classes for , Come and take our Post Model registration to the administration site . In this custom class , We define how to show Post Model .list_display Property allows you to set the model fields that you want to display on the manage object list page .

Now let's add other options for managing the site :

class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status')
list_filter = ('status', 'created', 'publish', 'author') search_fields = ('title', 'body')
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ['status', 'publish']

Reload the administration site , You can see :

You can see post The object list page shows you in list_display Define the fields displayed in .

The list page includes a right sidebar , Allow you to pass through in list_fliter Define filter fields to filter articles .

A search bar appears on the page . This is because we have used search_fields Property defines searchable fields .

Below the search bar , from date_hierarchy Attribute definitions , Provide a quick point to... Based on date hierarchy .

You can also see ,post Object according to Status and Publish Sort , You can still use it ordering Property defines the default sort method .

Now click Add post link , Get into post Object editing interface . When you put the newly created post Enter the title of the object title Field time , You'll find that ,slug Fields are automatically filled with the corresponding content . This is because we use prepopulated_fields Properties tell Django,slug yes title The associated fields of . in addition , Now the author field will display a find control , Because when you have hundreds of registered users , It's very inconvenient to choose from the drop-down list .

have a look , We only used a few lines of code , The management mode of the model is defined in the management site . Customization and expansion Django There are many ways to manage your site , In a later chapter , We'll tell you more about .

Link to the original text :

More tutorials :

Django1.8 course —— Build a complete... From scratch django Blog ( Two ) More articles about

  1. Django1.8 course —— Build a complete... From scratch django Blog ( One )

    first Django The project will be a complete blog site . It's the same blog we use in our blog Park , There are also categories . label . file . Query and other functions . If you are right about Django interested , This is a great opportunity . The tutorial will be with you , Starting from scratch , build ...

  2. Django1.8 course —— Build a complete... From scratch django Blog ( 3、 ... and )

    This section mainly introduces the access operation to the database : Through Manager (manage), Retrieving objects . modify . Delete and other operations , How to customize the manager for different models is introduced in detail . Inquiry and management work Now? , We already have a fully functional Django ...

  3. use express Build a simple blog system

    from : Express brief introduction Express It's a simple and flexible one node.js W ...

  4. Use Hexo Build a simple blog ( Two )

    Yesterday I wanted to use Hexo and github Set up a simple blog of your own , Record the pit you stepped in , The specific process will not be repeated , I mainly refer to several articles GitHub+Hexo Build a personal website detailed tutorial Use Hexo+Github Step by step, ...

  5. Use Hexo Build a simple blog ( One )

    After setting up a simple blog framework , Looking back, I found that , There is a trace of simplicity in simplicity , well , Online about rich hexo There are also a lot of articles Record some of your blind operations . In your hexo Under the table of contents , You can see themes There's a default in the folder land ...

  6. Use Hugo, just 5 minute , Easy to build your own blog

    I've introduced hexo This static blog system , Powerful , Basically meet the needs of the blog . today , Let me introduce an excellent static blog system , That's it Hugo. Hugo By Go Language implementation of static website generator . Simple . Easy to use . Efficient . ...

  7. Express Introductory tutorial : A simple blog

    come from : Express brief introduction Express It's a simple and flexible one node.js Web Application framework , ...

  8. Build a... From scratch react project

    Nav logo 120 Find out Focus on news 4 Search for Build a... From scratch react project 96 Thin people fake lulu 2017.04.23 23:29* Number of words 6330 read 32892 Comment on 31 like 36 Project location ...

  9. react The whole family started from 0 Build a complete react project (react-router4、redux、redux-saga)

    react The whole family started from 0 To 1( newest ) This article starts from scratch , Step by step explain how to use react Build a complete react project . For react.webpack.babel.react-route.redux.redu ...

Random recommendation

  1. Python Read and write files

    Python Read and write files Use open Remember to call the file object after opening the file close() Method . For example, you can use try/finally Statement to ensure that the file can be closed at the end . file_object = open('t ...

  2. ACM Yougth To maximize

    Yougth To maximize The time limit :1000 ms  |  Memory limit :65535 KB difficulty :4   describe Yougth Now there is n The weight and value of each item are Wi and Vi, You can help him pick out k The price per unit weight of each item is ...

  3. Big talk data structure ( Ten )java Program —— queue

    1. Definition of queue queue (queue): It is only allowed to insert at one end , A linear table with deletion at the other end . A queue is a first in, first out linear table , abbreviation FIFO(First out firts in). The end of the line is allowed to be inserted ...

  4. winform Form follows form

           Form2 frm2 = new Form2();         private void MoveProc()         {             frm2.StartPos ...

  5. SqlServer The database automatically executes the script on a regular basis

    demand : Database for automatic script operation , Some database operations do not need to write a program to execute regularly , You can give it to us SqlServer Client agent to do , Next steps , I'm combining with the previous article SqlServer Data transfer between different server databases ...

  6. Swift - character string (String) Usage details

    Following pair String Make a summary of the commonly used properties and methods 1, Determine whether it is null :isEmpty 1 2 3 var str:String if str.isEmpty{ } 2, Get the number of characters :countElements ...

  7. hive: insert Data time Error during job, obtaining debugging information as well as beyond physical memory limits

    insert overwrite table canal_amt1...... 2014-10-09 10:40:27,368 Stage-1 map = 100%, reduce = 32%, Cu ...

  8. qualcomm MSM8998 ABL Debugging of

    Qualcomm in MSM8998 On the introduction of UEFI, Used in place of LK(Little Kernel). qualcomm UEFI from XBL and ABL Two parts .XBL Responsible for core application functions such as chip driver and charging .ABL Including chip independent applications such as fastbo ...

  9. Springboot Integrate Quartz

    I learned before spring Scheduled tasks for : This paper mainly studies and records springboot Use quartz 1.   ...

  10. vue Upload and preview pictures , And clear the picture

    vue Write native upload image and preview and clear image effect , Here is demo, There are vue obtain input box value Value and vue Calls between functions in <!DOCTYPE html> <htm ...