python - Django (mezzanine) urls catching everything -
i'm writing custom views in admin of django project, should simple. have "events" page , want create "event" page (exactly same django polls tutorial in admin, event page same detail view.) no cannot use built in functionality normal using foreignkeys etc , need build scratch.
urls.py:
admin.autodiscover() def get_admin_urls(urls): def get_urls(): my_urls = [ url(r'^my_cms/events', views.events, name="events"), url(r'^my_cms/events/(?p<event_id>[0-9]+)/$', views.detail, name='detail'), ] return my_urls + urls return get_urls admin_urls = get_admin_urls(admin.site.get_urls()) admin.site.get_urls = admin_urls urlpatterns = i18n_patterns("", ("^admin/", include(admin.site.urls)), )
so.. visiting .../admin/my_cms/events/ works .../admin/my_cms/events/xxxxxx displays same events page, rather detail view
if change url pattern other "events" eg:
url(r'^my_cms/events', views.events, name="events"), url(r'^my_cms/[anything]/(?p<event_id>[0-9]+)/$',
then display event view correctly...
so question why first url catching everything? can put (.../admin/my_cms/events/anythingilike) , display events page?
joe
because r'^my_cms/events'
doesn't have $
@ end. means, beginning of url checked, not ending.
to illustrate, r'^my_cms/events'
match url begins 'my_cms/events'
, example:
'my_cms/events' 'my_cms/events/xxxxxx' 'my_cms/eventsxxxxxx'
so, if make request detail view @ 'my_cms/events/xxxxxx'
, events page url matched. once django finds match, doesn't check further urls , calls related view.
to fix, add $
sign @ end of url regexp, this:
url(r'^my_cms/events/$', views.events, name="events"),
Comments
Post a Comment