Regular expression url Django - python

I would like to have one or two urls in following format in django.
e.g. /view/q/node/c/programming....
Here q, c, etc are specific get params and latter on I'll be accessing them either through js or in view.
I have tried following pattern, but it didn't work out.
r'view/(P<q>\w+)/(P<c>\w+)/(P<l>\w+)/(P<o>\w+)/$'
I know that all the get params can be accessed in view if we have ? in url. But I don't want to have ? in url.
Can anyone suggest how to achieve this? Please note that the url can be without any params also. e.g. view/.
Thanks.

You're missing the ? from your Named groups. From the Django docs:
In Python regular expressions, the syntax for named regular-expression groups is
(?P<name>pattern), where name is the name of the group and pattern is some pattern to match.
Thus, referring to your example, it should be:
r'^view/(?P<q>\w+)/(?P<c>\w+)/(?P<l>\w+)/(?P<o>\w+)/$'

Related

Why urls order is important in django for different named urls?

I have two urls in my urls.py file
url('to_quotation/$', views.to_quotation, name='to_quotation'),
url('turn_into_quotation/$', views.turn_into_quotation, name='turn_into_quotation'),
and i have two view for them in views.py. When i make an ajax call to 'turn_into_quotation' url, 'to_quotation' view works. But if i changed my urls.py as:
url('turn_into_quotation/$', views.turn_into_quotation, name='turn_into_quotation'),
url('to_quotation/$', views.to_quotation, name='to_quotation'),
it works properly.
What is the reason for that?
You are missing the ^ at the beginning of the regex. Change it to:
url(r'^to_quotation/$', views.to_quotation, name='to_quotation'),
url(r'^turn_into_quotation/$', views.turn_into_quotation, name='turn_into_quotation'),
Without the ^, to_quotation/$ matches to_quotation/ and also turn_into_quotation/. In that case, the order matters, because Django will use the first URL pattern that matches.
If you're using a recent version of Django, you could use path() instead, and avoid regex gotchas.
path('to_quotation/', views.to_quotation, name='to_quotation'),
path('turn_into_quotation/', views.turn_into_quotation, name='turn_into_quotation'),
#Alasdair's answer is amazing~ I'd like to attach more infomations:
Django use regex-like syntax to match it and split url's argument:
^to-quotation/<id: int>/$`
It will: 1. try to match the url, 2. try to split its argument from url, and here it split int-value to id.
So it is easy to know, in the url's settings, it is important to hold each sub-url cannot match another.

Regular Expression on Django doesn't work

I'm relatively new to Django so I am doing this tutorial, but I encountered a problem with regular expressions:
For this view
def viewArticle(request, month, year):
text = "Displaying articles of : %s/%s"%(year, month)
return HttpResponse(text)
I am supposed to create a url like that
url(r'^articles/(\d{2})/(\d{4})', 'viewArticles', name='articles')
and it works perfectly, for example when I enter http://.../articles/12/2014 I get "Displaying articles of: 12 / 2014", as I should.
However, later (on page 27 of the PDF) I am advised to change the url to this:
url(r'^articles/(?P\d{2})/(?P\d{4})', 'viewArticles', name='articles'),
and now it doesn't work anymore. Why could that be and how can I change my code? Thanks for any suggestions!
The tutorial says that you may use named capturing groups here:
(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.
The correct declaration of a named capturing group is (?P<name>...):
url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})', 'viewArticles', name='articles')

Handling '/' in url match in Django

I am newbie to Django. I have written a function func() in views.py and corresponding regex for matching url in urls.py like following:
(r'callfunc/(?P<param1>(\w+|\d+|\-){0,50})/(?P<param2>(\w+|\d+|\-){0,50})/$', 'func')
The problem is that sometimes '/' appears inside param1 string and then regular expression matching fails in urls.py and application get stuck in the match. Is there any way to handle that properly so that even if any / comes inside param1, param1 get set properly?
How do you resolve an url with additional slash by hand? Let's say: callfunc/foo/bar/baz/. Does bar belong to param1 or param2? I think under this conditions it is not possible to distinguish the two params properly.
You can try to add a special disconnector: r'^callfunc/(?P<param1>[\w\d\-/]{1,50})/special-versus/(?P<param2>[\w\d\-/]{1,50})/$' It should be more or less impossible that the disconnector appears in on of your params.

Django Urlconf Named Groups String Matching

I'm trying to match two strings in an url, but it's not working out likely due to my poor knowledge of regex. I want urls like the following:
testserver/username/
testserver/username/1234/
testserver/username/rewards/
Username would be passed in to the url as a kwarg. Here's what I have:
url(r'^(?P<username>[-\w]+)/$', Posts_Index, name="userposts"),
url(r'^(?P<username>[-\w]+)/photos/$', Photo_Index, name="userphotos"),
url(r'^(?P<username>[-\w]+)/rewards/$', Rewards_Index, name="userrewards"),
url(r'^(?P<username>[-\w]+)/following/$', Follower_Index, name="userfollowers"),
url(r'^(?P<username>[-\w]+)/followers/$', Following_Index, name="userfollowing"),
url(r'^(?P<username>[-\w]+)/(?P<pk>\d+)/$', SinglePost_Index, name="singlepost"),
However, only userposts will be found. If I try to query userphotos or anything below userposts, only the userposts url will be checked, which obviously leads to failure. How can I fix this issue?
From the django docs: Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
Thus if you reverse the order of the urls it should work better.

Having trouble writing a url pattern in django

I want to match the following url pattern /posts/tagged/tag-name/
I've tried a few different patterns but Django splits out a 404 because it the url doesn't match any patterns.
This is the pattern I pulled from the docs http://www.webmonkey.com/2010/02/use_url_patterns_and_views_in_django/
(r'^posts/tagged/(?P[-w]+)/$', 'blog.view.posts_by_tag')
Can anyone one help me out?
Try this:
(r'^posts/tagged/(?P<tag>[-\w]+)/$', 'blog.view.posts_by_tag')
Your posts_by_tag view will then receive a keyword argument called tag.

Categories

Resources