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

时间:2021-04-08 21:11:27

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

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

mysite/
    templates/
        admin/
            index.html

Then add to apps block my code:

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

<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>

This works good, then I create new page /templates/admin/preferences/preferences.html and add to urls.py:

这很好,然后我创建新的页面/模板/管理/首选项/首选项。html并添加到url。py:

url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),

And add code to preferences.html:

并将代码添加到preferencese .html:

{% extends "admin/base_site.html" %}
{% block title %}Test page{% endblock %}

Run it and see message with error "The requested admin page does not exist.". What I do wrong?

运行它并查看错误消息“所请求的管理页面不存在”。我做错了什么吗?

4 个解决方案

#1


23  

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

您需要在管理本身的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')),
)

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

这样,这个URL将不会被Django的管理员处理。

#2


10  

You should be using admin's get_urls.

您应该使用admin的get_urls。

#3


5  

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:

这里有一个例子,说明在自定义管理页面中需要的所有东西(从Django 1.6开始)都可以从对象细节页面右上角的“History”按钮旁边的按钮链接到:

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

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

#4


0  

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)
        ]

#1


23  

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

您需要在管理本身的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')),
)

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

这样,这个URL将不会被Django的管理员处理。

#2


10  

You should be using admin's get_urls.

您应该使用admin的get_urls。

#3


5  

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:

这里有一个例子,说明在自定义管理页面中需要的所有东西(从Django 1.6开始)都可以从对象细节页面右上角的“History”按钮旁边的按钮链接到:

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

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

#4


0  

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)
        ]