如何为django admin创建自定义页面?

Gr1*_*r1N 38 python django django-admin

我想为没有模型的管理面板创建自定义页面.首先我将index.html复制到项目文件夹:

mysite/
    templates/
        admin/
            index.html
Run Code Online (Sandbox Code Playgroud)

然后添加到应用程序阻止我的代码:

<div class="module">
    <table summary="{% blocktrans with name="preferences" %}Models available in the preferences application.{% endblocktrans %}">
        <caption><a href="preferences" class="section">{% blocktrans with name="preferences" %}Preferences{% endblocktrans %}</a></caption>
            <tr>
                <th scope="row"><a href="preferences">Preferences</a></th>
                <td><a href="preferences" class="changelink">{% trans 'Change' %}</a></td>
            </tr>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

这很好用,然后我创建新页面/templates/admin/preferences/preferences.html并添加到urls.py:

url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),
Run Code Online (Sandbox Code Playgroud)

并将代码添加到preferences.html:

{% extends "admin/base_site.html" %}
{% block title %}Test page{% endblock %}
Run Code Online (Sandbox Code Playgroud)

运行它并查看错误消息"请求的管理页面不存在.".我做错了什么?

Sim*_*ser 30

您需要在管理员本身的URL模式之前添加管理URL :

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

这样,Django的管理员就不会处理这个URL.

  • @ICanKindOfCode 不,不是 (6认同)
  • 页面是受保护还是对公众开放? (3认同)

Mit*_*tar 12

你应该使用admin的get_urls.

  • `get_urls`是`ModelAdmin`的一种方法,它反过来需要一个`Model`,但OP特别想要"为没有模型**的管理面板**创建自定义页面".(重点补充.) (11认同)

Rui*_*lho 11

经过多年,仍然可以发布相关的答案.

使用Django 1.10+你可以:

security/admin.py(这是你应用的管理员文件)

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)
Run Code Online (Sandbox Code Playgroud)

安全/模板/ configuration.html

{% extends "admin/base_site.html" %}
{% block content %}
...
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

请参阅官方ModelAdmin.get_urls说明(确保选择正确的Django版本,此代码对于上面的1.10有效)


idl*_*ign 6

如果您想创建一个自定义页面只是为了放置任意表单来处理用户输入,您可以尝试django-etc。您etc.admin.CustomModelPage可以使用:

    # admin.py
    from etc.admin import CustomModelPage

    class MyPage(CustomModelPage):
    
        title = 'My custom page'  # set page title

        # Define some fields you want to proccess data from.
        my_field = models.CharField('some title', max_length=10)

        def save(self):
            # Here implement data handling.
            super().save()

    # Register the page within Django admin.
    MyPage.register()
Run Code Online (Sandbox Code Playgroud)


mat*_*rgo 5

以下是一个自定义管理页面应该需要的所有内容(从Django 1.6开始),该页面链接到对象详细信息页面右上角"历史记录"按钮旁边的按钮:

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

  • 这个问题的关键是_管理面板*没有*模型_,该解决方案与模型相关。 (2认同)

Mar*_*hyn 5

完整示例:

from django.urls import path
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 [
            path('my_view/', MyCustomView.as_view(), name=view_name)
        ]
Run Code Online (Sandbox Code Playgroud)

通过这种方法,Django 的makemigrations命令将创建数据库迁移来为 DummyModel 创建表。