How can I create custom page for django admin?

后端 未结 5 1562
日久生厌
日久生厌 2020-12-13 02:28

I want to create custom page for admin panel without model. For first i copy index.html to project folder:

mysite/
    templates/
        admin/
                     


        
5条回答
  •  青春惊慌失措
    2020-12-13 03:04

    Years go by and still a relevant answer to this can be posted.

    Using Django 1.10+ you can do:

    security/admin.py (this is your app's admin file)

    from django.contrib import admin
    from django.conf.urls import url
    from django.template.response import TemplateResponse
    from security.models import Security
    
    
    @admin.register(Security)
    class SecurityAdmin(admin.ModelAdmin):
    
        def get_urls(self):
    
            # get the default urls
            urls = super(SecurityAdmin, self).get_urls()
    
            # define security urls
            security_urls = [
                url(r'^configuration/$', self.admin_site.admin_view(self.security_configuration))
                # Add here more urls if you want following same logic
            ]
    
            # Make sure here you place your added urls first than the admin default urls
            return security_urls + urls
    
        # Your view definition fn
        def security_configuration(self, request):
            context = dict(
                self.admin_site.each_context(request), # Include common variables for rendering the admin template.
                something="test",
            )
            return TemplateResponse(request, "configuration.html", context)
    

    security/templates/configuration.html

    {% extends "admin/base_site.html" %}
    {% block content %}
    ...
    {% endblock %}
    

    See Official ModelAdmin.get_urls description (make sure you select proper Django version, this code is valid for 1.10 above)

    • Note the use of get_urls() above.
    • This new admin page will be accessible under: https://localhost:8000/admin/security/configuration/
    • This page will be protected under admin login area

提交回复
热议问题