How can I create custom page for django admin?

后端 未结 5 1552
日久生厌
日久生厌 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
    0 讨论(0)
  • 2020-12-13 03:14

    You need to add your admin URL before the URL patterns of the admin itself:

    urlpatterns = patterns('',
       url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),
       url(r'^admin/', include('django.contrib.admin.urls')),
    )
    

    This way the URL won't be processed by Django's admin.

    0 讨论(0)
  • 2020-12-13 03:19

    You should be using admin's get_urls.

    0 讨论(0)
  • 2020-12-13 03:23

    Here's an example of everything that should be needed (as of Django 1.6) for a custom admin page that is linked to from a button next to the "History" button in the top right of an object's detail page:

    https://gist.github.com/mattlong/4b64212e096766e058b7

    0 讨论(0)
  • 2020-12-13 03:24

    Full example:

    from django.conf.urls import url
    from django.contrib import admin
    from django.db import models
    
    class DummyModel(models.Model):
        class Meta:
            verbose_name = 'Link to my shiny custom view'
            app_label = 'users'  # or another app to put your custom view
    
    @admin.register(DummyModel)
    class DummyModelAdmin(admin.ModelAdmin):
        def get_urls(self):
            view_name = '{}_{}_changelist'.format(
                    DummyModel._meta.app_label, DummyModel._meta.model_name)
            return [
                url(r'^my_view/$', MyCustomView.as_view(), name=view_name)
            ]
    

    With this approach Django's makemigrations command will create DB migration to create table for DummyModel.

    0 讨论(0)
提交回复
热议问题