How do I allow my django admin console URL to take priority?

心已入冬 提交于 2019-12-25 05:16:18

问题


I am currently using a system where anybody can see a user's public profile when typing in /{username} as a URL.

This has somehow taken priority so therefore I cannot anymore access the admin console /admin.

How do I make /admin take priority?

urls.py

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

admin.autodiscover()

urlpatterns = patterns('',
(r'^player/', include('player_details.urls')),
(r'^profile/', include('userprofile.urls')),
url(r'^admin/', include(admin.site.urls)), # enable administration
url(r'^(?P<username_in_url>\w+)/$', 'fantasymatchday_1.views.public_profile'), #user public profile logged in
url(r'^(?P<username_in_url>\w+)/$', 'fantasymatchday_1.views.public_profile_anon'), #user public profile anonymous
)

The URL's going to the player and profile apps work fine, what am I doing wrong?


回答1:


Your diagnosis of the problem is wrong. The error you quote in the comment to Simeon does not indicate that the admin URL is going to the profile page: it wouldn't, anyway, because the admin pattern already comes before the profile one in your urls.

The error is, however, quite clear: the view function "fantasymatchday_1.views.public_profile_anon" does not exist. Either create it, or remove the reference to it from the urls. (It seems completely irrelevant anyway, as it has exactly the same url as the one before it, so it will never be invoked. It needs to exist anyway if you reference it from urlpatterns, though.)




回答2:


You need to put the admin URL first:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)), # enable administration
    ...

This means it'll match against that URL first and for the word "admin" it'll then display the admin panel. For other URLs it'll continue and it'll match player names against those URLs.



来源:https://stackoverflow.com/questions/25569728/how-do-i-allow-my-django-admin-console-url-to-take-priority

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!