我想为没有型号的管理面板创建自定义页面。首先我将index.html复制到项目文件夹:
mysite/
templates/
admin/
index.html
然后添加到应用程序阻止我的代码:
<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>
这很好用,然后我创建新页面/templates/admin/preferences/preferences.html和 添加到urls.py:
url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),
并将代码添加到preferences.html:
{% extends "admin/base_site.html" %}
{% block title %}Test page{% endblock %}
运行它并查看错误消息“请求的管理页面不存在。”。我做错了什么?
答案 0 :(得分:29)
您需要在管理员自己的网址模式之前添加管理网址:
urlpatterns = patterns('',
url(r'^admin/preferences/$', TemplateView.as_view(template_name='admin/preferences/preferences.html')),
url(r'^admin/', include('django.contrib.admin.urls')),
)
这样,Django的管理员不会处理该URL。
答案 1 :(得分:11)
您应该使用管理员get_urls。
答案 2 :(得分:6)
岁月流逝,仍然可以发布一个相关的答案。
使用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)
security / templates / configuration.html
{% extends "admin/base_site.html" %}
{% block content %}
...
{% endblock %}
请参见Official ModelAdmin.get_urls description(确保您选择了正确的Django版本,此代码对上述1.10有效)
答案 3 :(得分:5)
以下是一个自定义管理页面应该需要的所有内容(从Django 1.6开始),该页面链接到&#34;历史记录&#34;旁边的按钮。对象详细信息页面右上角的按钮:
答案 4 :(得分:2)
完整示例:
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)
]