The Django Admin site combined with ORM provides a very powerful database operation function, and a small amount of code can quickly realize the display, modification, saving of the database table data visualization pages and functions.
The default display of the Django Admin page is divided into multiple app modules, and the database model defined below each app module is presented as a hyperlink to the table name, such as a default only one auth module when the Django service is initialized. Following the users and groups two tables, adding the user-defined feedback module and the model of the feedback table, the admin site page looks like this:
Sometimes, we need to define some custom pages for the feedback app, providing features that the admin site+orm cannot directly provide, such as summarizing the overall processing of different types of feedback on a daily basis, based on the records in the Feedbacks table, and then need the admin Add a jump link under the corresponding app module in the Site page. Here are two steps to record the implementation step.
1, customizing the Django template page
To write a simple feedback_stats.html file that uses the Django template language, the file must be located under the default path of the Django-loaded HTML file, which is chosen under feedback/templates/. The following HTML file generates a data presentation table based on the incoming theads and Trows parameters:
{% extends "admin/base_site.html"%}{% load static%}{% block content%}<Div> <label><H2>Feedback statistics</H2> </label> </BR> <Tableclass= "Table"Table-layout= "fixed"width= "600px"> <thead> <TR>{% for thead in theads%}<th>{{THEAD}}</th>{% endfor%}</TR> </thead> <tbody>{% for TR in trows%}<TR>{% for TD in TR%}<TD>{{TD | safe}}</TD>{% endfor%}</TR>{% endfor%}</tbody> </Table> </Div>{% Endblock%}
In the views.py file, the HTML file that contains the template syntax is rendered as a pure HTML Web page through the Django.shortcuts.render function, and the computed data record is passed to the render function as a key-value parameter. The contents of the feedback/views.py file are as follows:
#!/usr/bin/env python#Coding=utf-8 fromDjango.shortcutsImportRenderImportMysqldbconn=MySQLdb.connect (Host='localhost', Port=3306, the user='Root', DB='Test') Dtype={0:u'Unknown category', 1:u'cannot play', 2:u'violation Report', 3:u'Quality issues',}dstatus={0:u'Pending Confirmation', 1:u'Pending Resolution', 2:u'has been resolved', 3:u'no need to solve',}deffeedback_stats (Request): Cursor=conn.cursor () cursor.execute ('Select Defect_type, Status, Count (1) as CNT, left (CTime, ten) from feedback Group by Defect_type, status, left (CTime, ORDER BY CTime Desc;') Records= [(Dtype[x[0]], dstatus[x[1]], x[2], x[3]) forXinchCursor.fetchall ()] Theads= ['Feedback Type','Current status','Quantity','Time'] returnRender (Request,'feedback_stats.html', {"theads": Theads,"Trows": Records})
feedback/views.py
The contents of the feedback/urls.py file are as follows:
#!/usr/bin/env python# coding=utf-8from django.conf.urls import URL Import viewsurlpatterns = [ url (r ' ^feedback_ stats/$ ', views.feedback_stats), url (r ' ^admin/feedback_stats/$ ', views.feedback_stats),
feedback/urls.py
The following data exists in the Database feedback table:
| ID | video_id | Udid | Defect_type | message | CTime | Total | Status |+----+----------+----------+-------------+----------+---------------------+-------+--------+| 1 | 1 | udid1234 | 3 | Testtext | 2017-11-21 02:38:11 | 55 | 1 | | 2 | 1 | udid1234 | 3 | Testtext | 2017-11-21 02:53:13 | 55 | 1 | | 3 | 1 | udid1234 | 0 | Testtext | 2017-11-21 02:53:18 | 55 | 2 | | 4 | 1 | udid1234 | 0 | Testtext | 2017-11-21 02:53:23 | 55 | 2 | | 5 | 11 | udid1234 | 0 | Testtext | 2017-11-21 02:55:30 | 55 | 2 | | 6 | 19 | udid1234 | 0 | Testtext | 2017-11-21 02:55:35 | 55 | 0 | | 7 | 19 | udid1234 | 0 | Testtext | 2017-11-21 03:06:43 | 55 | 0 | | 8 | 19 | udid1234 | 0 | Testtext | 2017-11-28 00:09:45 | 55 | 0 | | 9 | 11 | udid1234 | 0 | Testtext | 2017-11-28 00:09:58 | 55 | 3 | | 10 | 101 | udid1234 | 1 | Testtext | 2017-11-28 00:10:04 | 5 | 3 | | 11 | 121 | udid1234 | 1 | Testtext | 2017-11-28 00:10:14 | 75 | 3 | | 12 | 77 | udid1234 | 1 | Testtext | 2017-11-28 00:10:24 | 7 | 3 | | 13 | 27 | udid1234 | 2 | Testtext | 2017-11-28 00:10:33 | 91 | 3 | | 14 | 27 | udid1234 | 2 | Testtext | 2017-11-28 00:13:11 | 91 | 0 | | 15 | 32 | udid1234 | 2 | Testtext | 2017-11-28 00:13:18 | 91 | 0 |+----+----------+----------+-------------+----------+---------------------+-------+--------+
Feedback Table Records
Access http://127.0.0.1:8000/feedback/feedback_stats/to view problem status statistics that differentiate the type of feedback:
Here, we have implemented the function of customizing the template statistics page, and can be accessed through the path of/feedback/feedback_stats/or/admin/feedback/feedback_stats/. However, this link does not appear on the Admin Site page, the admin page under the Feedback app module, or only a jump to the Feedback Table Admin page hyperlink, the slightest can not find a jump to feedback_stats page hyperlink , it appears that you manually add a sub-path directly after the admin/feedback/path, and Django does not intelligently add the corresponding jump hyperlink in the Admin page. If such a custom page connection can not be displayed on the admin page of a corresponding hyperlink, each use need to manually enter the entire path, is undoubtedly very cumbersome, that Django can be done in the Admin page to add user-defined page hyperlink? The answer is of course yes, see the Django Implementation custom Template page and add a custom jump link in the Admin Site App module (ii).
Django implements a custom template page and adds a custom jump link in the Admin Site App module (i)