Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin urls

I would like to use administration website directly by reverse url in my Django application but I get this error : No module named admin.site.urls

I have urls.py file which looks like :

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

And one of my HTML template has :

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

Something is wrong ? I don't find How I can solve this error ..

like image 523
Essex Avatar asked May 23 '17 09:05

Essex


1 Answers

In Django 1.9+, you don't need to use include. Use the callable admin.site.urls, not the string 'admin.site.urls'.

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

In Django 2.0+, you can use path() instead of url().

path('admin/', admin.site.urls),

In Django < 1.9 you pass admin.site.urls to include.

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

To reverse the admin index url, change the url tag in your template to:

{% url "admin:index" %}
like image 134
Alasdair Avatar answered Nov 16 '22 17:11

Alasdair