Parenthesis URL dispatcher Django? - python

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

Related

Django 1.11 url pattern error, how to solve?

When I try to fix the url in my urlpatterns it shows me this error :
The error:
Your URL pattern "url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),"
is invalid. Ensure that urlpatterns is a list of url() instance.
try removing the string 'url(r'^player/[?P[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),'. The list of urlpatterns should not have a prefix string as the first element.*
My Code :
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', HomePageView.as_view(), name='home-page'),
url(r'^teams/$', TeamsListView.as_view(), name='teams-list-view'),
url(r'^scores/$', ScoresListView.as_view(), name='scores-list-view'),
url(r'^player/[?P<slug>[-\w\x20]+]/$', PlayerDetailView.as_view(), name='player-detail-view'),
]
Can anyone help me??
The syntax is a bit off, you need to use round brackets (..) instead of square brackets [..] around a "capture group":
url(
r'^player/(?P<slug>[-\w\x20]+)/$',
PlayerDetailView.as_view(),
name='player-detail-view'
),
Furthermore if I recall correctly, a slug can not contain spaces, so you might want to remove the \x20.
Note that in django-2.0 and higher, the path(..) [Django-doc] function can be used, which has support for slugs like:
# Django 2.0 and higher
path('player/<slug:slug>/', PlayerDetailView.as_view(), name='player-detail-view'),
Then Django replaces the slug with a builtin pattern, which makes the URL patterns more "declarative".

Django url slash matching

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.

Django app urls not working properly

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.

django urls redirect me to other view

I don't understand my problem here is my code:
urls.py
urlpatterns = patterns('blog.views',
...
url(r'^(?P<slug>.+)$', 'blog', name='blog'),
url(r'^membres$', 'membres', name='membres'),
)
views:
def blog(request, slug):
posts = Post.objects.filter(blog__slug=slug)
return render(request, 'blog/blog.html', locals())
def membres(request):
membres = User.objects.all()
return render(request, 'blog/membres.html', {'membres': membres})
Here is my link in my base.html template
<li>List</li>
When I click the link it redirect me to the blog view and then render blog.html instead of using membres view.
I got no error in console or in my template.
All my code is in my app called 'blog'
Django uses the first pattern that matches. Your first URL regex matches any string, including /membres, so Django never tries the second one. I suggest something like this:
urlpatterns = patterns('blog.views',
url(r'^/blog/(?P<slug>[-\w]+)/$', 'blog', name='blog'),
url(r'^membres/$', 'membres', name='membres'),
)
If you must have a catch-all pattern, it should be the last one in the list, so the other patterns have a chance to match before:
urlpatterns = patterns('blog.views',
url(r'^membres/$', 'membres', name='membres'),
# other patterns...
url(r'^(?P<slug>[-\w]+)/$', 'blog', name='blog'),
)
It's also a good habit to always include the trailing slash (Django will append it to requests by default). To match a slug, I suggest [-\w]+, which will match any sequence of alphanumeric characters, _ and -.
It's because urlresolver takes patterns from top to bottom and 'membres' matches (?P<slug>.+) so urlresolver returns blog view. Put more concrete urlpatterns higher. Also I suggest using more specific characters in slug regexp, i.e. (?P<slug>[A-Za-z0-9_\-]+).
Django stops at the first URL pattern that matches. This means that your
blog view -- which simply looks for one or more characters -- will interpret your mysite.com/membres URL to be a blog post with the slug membres.
To fix it, try swapping the order of your URL patterns:
urlpatterns = patterns('blog.views',
...
url(r'^membres$', 'membres', name='membres'),
url(r'^(?P<slug>.+)$', 'blog', name='blog'),
)
In general, you want your most general patterns at the bottom for exactly this reason.

why is this django regex wont work?

I have this regex in my urls.py for my blog app and I'd like to know why is it not working.
url(r'^/tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),
and I have defined this in the blog's views.py
def tag(request,tag_text):
and this in the application's urls.py
url(r'^blog/', include('blog.urls')),
I have tried
localhost/blog/tag/sport
but I still get: The current URL, blog/tag/sport, didn't match any of these.
Am I doing something wrong?
Your pattern is trying to match an extra /, since your include url requires a trailing slash, and your tag url is trying to match a leading slash.
You should remove either one to make it work:
# tag url in blog/urls.py
url(r'^tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),
# include in project/urls.py
url(r'^blog/', include('blog.urls')),

Categories

Resources