简体   繁体   中英

Django admin site URL routing issues/inconsistencies

I am working on a website on Django 1.10, and I am trying to set up my admin site so the URL is at "/pcari/admin" instead of "/admin"

Here is my root urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    # Admin site
    url(r'^admin/', admin.site.urls),

    # Regular site
    url(r'^pcari/', include('pcari.urls')),
]

Here is my app urls.py:

from django.conf.urls import url
from django.contrib import admin

from . import views

app_name = 'pcari'
urlpatterns = [
    # Admin site
    url(r'^admin/', admin.site.urls),

    # User-facing views
    url(r'^$', views.index, name='index'),

    ...

]

This setup technically works in that if I visit "127.0.0.1:8000/pcari/admin" I get the admin site, but I also get the admin site if I visit "127.0.0.1:8000/admin", which is something I do not want.

However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":

NoReverseMatch at /pcari/admin/
u'admin' is not a registered namespace
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/pcari/admin/
    Django Version: 1.10.5
    Exception Type: NoReverseMatch
    Exception Value:    

in your main urls.py

urlpatterns = [
    # Admin site
    url(r'^pcari/admin/', admin.site.urls),

    # Regular site
    url(r'^pcari/', include('pcari.urls')),
]

and remove the admin url from the app urls

However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":

 NoReverseMatch at /pcari/admin/
    u'admin' is not a registered namespace
        Request Method: GET
        Request URL:    http://127.0.0.1:8000/pcari/admin/
        Django Version: 1.10.5
        Exception Type: NoReverseMatch
        Exception Value:

Reason:

When you remove url(r'^admin/', admin.site.urls), from main urls.py that means you don't have any admin namespace becuase this line registered "admin" namespace that is define in django code. https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L279

this line show django admin will register as "admin" namespace

When you removed and added admin site in "pcari" app that means this app is register namespace "pcari". But in django admin template has hardcode url in template like {% url admin:index %} that gives NoReverseMatch Exception https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base.html#L44 there is hardcode admin namespace in django templates that cause error

EDIT:

You added in app urls

app_name = 'pcari'

That's creating this Noreverse Problem. Remove this app name line. Because it will work as pacri namespace if you add this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM