Django管理员网址

时间:2017-05-23 09:17:02

标签: django django-admin

我想在我的Django应用程序中直接使用反向网址管理网站,但我收到此错误:No module named admin.site.urls

我有urls.py文件,看起来像:

import os
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

from BirthCertificate import views
from Identity import views
from Accueil import views
from log import views
from Mairie import views
from Table import views
from Recensement import views
from Configurations import views
from Mariage import views

import debug_toolbar

from django.views.generic.base import TemplateView


urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name=os.path.join(settings.BASE_DIR, 'Accueil/templates/Choice.html')),
        name='choice'),
    url(r'^admin/', include('admin.site.urls')),
    url(r'^BirthCertificate/', include('BirthCertificate.urls')),
    url(r'^Identity/', include('Identity.urls')),
    url(r'^Accueil/', include('Accueil.urls')),
    url(r'^Home/', include('log.urls')),
    url(r'^Mairie/', include('Mairie.urls')), 
    url(r'^Table/', include('Table.urls')), 
    url(r'^Recensement/', include('Recensement.urls')),
    url(r'^Configurations/', include('Configurations.urls')),
    url(r'^__debug__/', include(debug_toolbar.urls)),
    url(r'^Mariage/', include('Mariage.urls')),
] 

我的一个HTML模板有:

{% if request.user|has_group:"admin" %}
    <li><a href="{% url "admin" %}" ><span class="glyphicon glyphicon-log-out"></span> Administration </a></li>
{% endif %}

出了什么问题? 我找不到如何解决这个错误..

1 个答案:

答案 0 :(得分:13)

In Django 1.9+,您无需使用include。使用可调用的admin.site.urls,而不是字符串'admin.site.urls'

url(r'^admin/', admin.site.urls),

In Django < 1.9您将admin.site.urls传递给include

url(r'^admin/', include(admin.site.urls)),

要反转管理员索引网址,请将模板中的网址标记更改为:

{% url "admin:index" %}