Django implements a custom template page and adds a custom jump link in the admin site's app module (ii)

Source: Internet
Author: User

In the article Django implements the custom template page and adds a custom jump link in the Admin Site App module (a) we succeeded in/feedback/feedback_stats/ The path customizes the HTML file using the template syntax, completing the function of jumping to the custom statistics page via the corresponding link, but not being able to add the displayed jump link successfully under the Feedback app module in admin site. Makes it easy for users to jump past the admin site page when they want to access a custom link.

Django as a widely used, already quite mature web framework, of course, should support the Admin Site page to add custom link features, but Django Novice did not read the relevant information, it is difficult to think how to achieve this function, I have been on the internet for a long time to find information , do not know whether the problem is not described, or the information is really very few, and did not find the right way to achieve the online, recently through the advice of colleagues, it dawned on me.

Idea: To add a custom jump link in admin site, first consider how to add a new jump link under the corresponding app, here take the feedback module as an example, what will result in a new jump connection? The most common scenario is obviously to add a model that maps to the database, and the corresponding modeladmin, so is it possible to redirect the link to the corresponding data table model to our custom link? This problem is converted to the admin site modeladmin Jump link can be overloaded, this is the admin site under an app module to add a custom jump link solution, that is, modeladmin default jump behavior to rewrite, Make it point to a custom jump page to enable the ability to add custom jump links under the app module.

Because the corresponding data table model actually only provides a function to generate a jump link under the admin site, it is not actually accessed, so there is no need to define any actual fields for it, construct an empty fake model, the following is the corresponding fake in models.py The Model code:

class Feedback_stat (models. Model):    Pass

This is the code corresponding to Feedbackstatsadmin in admin.py, which overrides the Changelist_view function that modeladmin responsible for the default page rendering behavior, which implements rendering of the custom page:

 from Import Admin  from Import Render  from Import Feedback, Feedback_stat  from Import Feedback_stats@admin.register (Feedback_stat) class feedbackstatsadmin (admin. Modeladmin):    def Changelist_view (self, request, extra_content=None):          return feedback_stats (Request)

After adding the code as above, the Admin Site page is as follows:

Where Feedback_stats is the link to the corresponding feedbackstatsadmin, after the click, because the rewrite of its Changelist_view method, it will jump to the user-defined Feedback_stats page:

Django implements a custom template page and adds a custom jump link in the admin site's app module (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.