my url pattern in apps like:
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
the url : /show/in_arg1/in_arg2%2F_this_is_arg2 match to view_3
but I want to let the url match view_2
I try to change urlpatterns like
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.\/]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.\/]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
the url : /show/in_arg1/in_arg2%2F_this_is_arg2 works well
but the url /show/in_arg1/in_arg2/in_arg3 will match to view_2, not what I want
It seems django decode %2F to / before url matching
Can I let django do url matching without decode %2F ?
Or some way to solve my problem
thanks
It's the \/in (?P<arg2>[\w\d_\.\/]+) that's messing you up. They would even match show/a//b!
Try these:
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
Somebody more savvy in regex might help you further.
Related
I have url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic') in urls.py but when I try to go to localhost:8000/topics/1 it tells me that it tried one pattern: topics/(?P<topic_id>**\\**d+)/$
I would think it would be topics/(?P<topic_id>**\**d+)/$
I'm using a book called The Python Crash Course (1st edition)(ch. 18).
This is a local server using Django 1.11 with Python. I've tried a lot of reformatting on the url pattern but I am new at this so I don't know what else to do.
...
urlpatterns = [
url(r'^$', views.index, name='index'),
# Show all topics.
url(r'^topics/$', views.topics, name='topics'),
# Detail page for a single topic.
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic'),
]
I expected it to pop up with the correct page but it always says 'NoReverseMatch at /topics/01/'
So you've forgotten the trailing slash at the end of your URL, hence why it's not matching.
You could remove the slash from the regex, but that would shift the problem: it wouldn't work if you put a slash.
I guess you could end the pattern with /?$, but here's a solution that's probably more robust: Jiaaro's answer to: django urls without a trailing slash do not redirect
Basically:
check your APPEND_SLASH setting in the settings.py file
I have been trying to load my localhost:8000/streamers/1234 however there is a bug in my urls that I cannot seem to fix. Iv tried both patterns below and I keep getting the error:
Django tried these URL patterns, in this order:
^streamers/(?P[0-9]+)/$ [name='streamer']
The current path, streamers/34/, didn't match any of these.
urlpatterns = [
#path(r'^streamers/<int:id>/', views.streamer, name='streamer'),
url(r'^streamers/(?P<id>[0-9]+)/$', views.streamer, name='streamer'),
]
if "views.streamer" is a class based view, use:
path(r'^streamers/<int:id>/', views.streamer.as_view(), name='streamer'),
Notice the "as_view()" after views.streamer.
I have the following base urls file:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^agenda/', include('planner.urls', namespace='planner', app_name='planner'))
]
And my planner app contains the following urls:
urlpatterns = patterns('',
url(r'^', SkirmList.as_view(), name='agenda'),
url(r'^skirm/(?P<pk>\d+)/$', SkirmDetailView.as_view(), name='skirmdetailview'),
)
The problem I am having is when going to: http://localhost:8000/agenda/skirm/41/
It keeps loading the SkirmList view instead of the SkirmDetailView.
It's propably obvious to most but I am a beginner with Django. Any help is appreciated, thanks
The regex r'^' matches any string. It just says the string needs to have a start. Every string has a start, so...
You need to include an end anchor as well:
url(r'^$', ...)
This regex looks for the start of the string, followed immediately by the end, i.e. an empty string. It won't match the /agenda/skirm/41/ url.
I remember , by default, any request to a URL that doesn’t match a URLpattern and doesn’t end with a slash will be redirected to the same URL with a trailing slash.But I occurred a problem.
the URLConf in my project :
#in urls.py file
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^cms/', include('cms.urls'))
)
#in the cms/ulrs.py
urlpatterns = patterns('cms.views',
url(r'^category/(?P<slug>[-\w]+)/$', 'category', name="cms-category"),
url(r'^search/$', 'search', { 'template' : 'cms/story_list.html'}, name="cms- search"),
)
If I typed: 127.0.0.1:8080/cms/search/ worked.
But if I type 127.0.0.1:8080/cms/search , then 404 occurred.
I just wonder why this problem existed? Can anyone give me some tips? thank you !
The easiest way to fix this would be to check the value of APPEND_SLASH in your settings.py file as suggested by John Wang. This will work only when CommonMiddleware is installed.
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-APPEND_SLASH
Alternatively, you can add a question mark at the end of the URL Pattern in order to make the slash optional.
url(r'^search/?$', 'search', { 'template' : 'cms/story_list.html'}, name="cms- search"),
This way both '127.0.0.1:8080/cms/search/' and '127.0.0.1:8080/cms/search' will work.
It is helpful since even if we only link to one format we know people will probably visit both, either by entering the URL manually or by linking from an external source. But these are considered as two distinct URLs by search engines.
This can be tackled by doing something like the following in your URL Patterns:
url(r'^search$', redirect_to, {'url':'/search/'}),
url(r'^search/?$', 'search', { 'template' : 'cms/story_list.html'}, name="cms- search"),
I have a parenthesis in my url :
Ex: http://en.wikipedia.org/wiki/Sacramentum_(oath)
Where The URL has parenthesis.
Django give """Page not found (404)"""
Any idea how to solve this problem ????
I'm guessing you have a URL pattern in urls.py that looks something like this (to use your example):
urlpatterns = patterns('',
url(r'^wiki/Sacramentum_(oath)/', my_view),
)
This won't match, because parentheses have a special meaning in regular expressions. You need to escape them with backslashes:
urlpatterns = patterns('',
url(r'^wiki/Sacramentum_\(oath\)/', my_view),
)