Django 2.0 - 将管理模板添加到常规视图

时间:2018-06-14 09:39:48

标签: python django django-templates django-views

我尝试将Django Administration的模板添加到具有职员权限的简单应用视图中。我在视图的顶部添加了{% extends "admin/base_site.html" %},但标题的某些部分缺失了。

正常管理标题:

enter image description here

使用管理模板测试视图:

enter image description here

程序my_app / urls.py:

from django.urls import path

from . import views

urlpatterns = [
    path('test/', views.test, name='test')
]

程序my_app / views.py:

from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render

@staff_member_required
def test(request):
    return render(request, 'test.html')

程序my_app /模板/的test.html:

{% extends "admin/base_site.html" %}

{% block content %}
<p>Test</p>
{% endblock %}

MY_PROJECT / urls.py:

from django.contrib import admin
from django.urls import include, path
admin.autodiscover()

admin.site.site_header = "Test 1"
admin.site.index_title = "Test 2"
admin.site.site_title = "Test 3"

urlpatterns = [
    path('my_app/', include('my_proj.my_app.urls')),
    path('', admin.site.urls),
]

谢谢!

0 个答案:

没有答案