My problem is the following:
Inside my urls.py I have defined these url patterns:
url(r'^image/upload', 'main.views.presentations.upload_image'),
url(r'^image/upload-from-url', 'main.views.presentations.upload_image_from_url'),
the problem is when I call from my browser the URL
myowndomain:8000/image/upload-from-url
Django always execute the first pattern (r'^image/upload')
Is there any solution to my problem?
Django uses the first matching pattern, and your ^image/upload pattern doesn't include anything to stop it matching the longer text. The solution is to require that your pattern also match the end of the string:
r'^image/upload$'
By convention, Django URLs generally have a trailing slash as well, but that's not strictly required:
r'^image/upload/$'
You need to insert the dollar sign "$" at the end of the pattern. The dollar sign is a character that represents position. In the case of regex, this is the end of the string. Because both image/upload and image/upload-from-url match what you're looking for, you need to explicitly say where to stop in the pattern.
Related
I have the one url like this
url(ur'^gradebook/(?P<group>[\w\-А-Яа-я])$', some_view, name='some_view')
and I expect it to process a request like
../gradebook/group='Ф-12б'
but I get an error and the server crashes.
Please help me figure out the Russian symbols
The group='…' part is more a problem, since the equation sign = is not part of the character group.
Furthermore you should match multiple characters:
# quantifier ↓
url(ur'^gradebook/(?P[\w\-А-Яа-я]+)$', some_view, name='some_view')
then this can match a URL:
/gradebook/Ф-12б
but if you want to match the group='…' as well, you should include the = and the ' character:
# extra characters ↓↓
url(ur"^gradebook/(?P[\w\-А-Яа-я'=]+)$", some_view, name='some_view')
Then you can match with:
/gradebook/group='Ф-12б'
although that might accept too much, since it can also accept f'q'a=gr=f for example.
Say you have a full url of localhost:thisdir/callview/
I have noticed that in a urls.py file, an included namespace is written as:
(r'^thisdir/', include('thisdir.urls', namespace='thisdir)),
where a beginning string is checked, but not end, and a view call is done as:
(r'^callview/$', 'thisdir.views.index', name='myview')
with the $ to check end of string. If the include pattern breaks "thisdir/" off the full url to check for that section first, and I thought it checks each string section at a time (so "thisdir/" is at the end of string) why do I never see (r'^thisdir/$', ...)
thank you
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
If you terminated an include with a $, then the rule would no longer match anything in the included file, because it would only match URLs ending in exactly the include regex.
The reason you never see it is because it would prevent the purpose of a URL include.
(r'^thisdir/$', <include>) This would only match urls equal to thisdir/ due to the terminating $. Therefore a url such as thisdir/foobar/ will not match and never be processed by an include.
On the other hand, if you leave the $ out of the regex, /thisdir/<anything> will match the regex and therefore can be processed further by the included urls.
I'm new to Django framework and learning it; many time I get the url patterns in urls.py as given below
url(r'^tracking/(?P<some_slug>[\w.-]+)/(?P<mail_64>{})/$'.format(base64_pattern), 'tracking_image_url', name='tracking_image_url'),
I understand the part P but after that [\w.-]+ is added or sometimes its simply w+.
Please can anyone make me understand these terms what they are? and for what they stand?
\w is a regular expression which matches any alphanumeric character and underscore. So, \w+ matches repeated alphanumeric characters (and underscores) and [\w-]+ adds the - to the set of matchable characters.
I have a django application hosted with Apache. I'm busy using the django restframework to create an API, but I am having issues with URLs. As an example, I have a URL like this:
url(r'path/to/endpoint/(?P<db_id>.+)/$', views.PathDetail.as_view())
If I try to access this url and don't include the trailing slash, it will not match. If I add a question mark on at the end like this:
url(r'path/to/endpoint/(?P<db_id>.+)/?', views.PathDetail.as_view())
This matches with and without a trailing slash. The only issue is that if a trailing slash is used, it now gets included in the db_id variable in my view. So when it searches the database, the id doesn't match. I don't want to have to go through all my views and remove trailing slashes from my url variables using string handling.
So my question is, what is the best way to make a url match both with and without a trailing slash without including that trailing slash in a parameter that gets sent to the view?
Your pattern for the parameter is .+, which means 1 or more of any character, including /. No wonder the slash is included in it, why wouldn't it?
If you want the pattern to include anything but /, use [^/]+ instead. If you want the pattern to include anything except slashes at the end, use .*[^/] for the pattern.
The .+ part of your regex will match one or more characters. This match is "greedy", meaning it will match as many characters as it can.
Check out: http://www.regular-expressions.info/repeat.html.
In the first case, the / has to be there for the full pattern to match.
In the second case, when the slash is missing, the pattern will match anyway because the slash is optional.
If the slash is present, the greedy db_id field will expand to the end (including the slash) and the slash will not match anything, but the overall pattern will still match because the slash is optional.
Some easy solutions would be to make the db_id non greedy by using the ? modifier: (?P<db_id>.+?)/? or make the field not match any slashes: (?P<db_id>[^/]+)/?
Hi all,
How does this expression actually work?
urlpatterns = patterns('',
url(r'^get/(?P<app_id>\d+)/$', 'app.views.app'),
...
)
I understand what it does, at least to map a url entered by the user to the app() function in the app's view page. I also understand it is a regular expression that ends up taking the id of the app and mapping it to the url. But where is this function going? What is going on with the r'^...?P /$ (I get the d+ is a digit regex, of the id itself, but that's about it).
I also understand this url function draws from the django.conf.urls module.
Perhaps my misunderstanding is more buried in my lack of regex experience. Nonetheless, I need help! I do not like using things I do not understand, and I am guilty.
Let's take a look: r'^get/(?P<app_id>\d+)/$'
The r'' means that assume as string characters every character inside the string quotes.
^ character means the beginning of the regular expression. For example, forget/123 won't match the expression because doesn't start with get, if the sign weren't there, it should've match it because it won't be forcing the matched string to begin with get, just that get...appears in the string.
The $ character means the end of the expression. If absent, get/123/xd may match the expression and this is not desired.
(?P<>) is a way to give a name/alias to a group in the expression.
You should read the python's regular expressions documentation. It's very good to know about regular expressions because they're very useful.
Hope this helps!
r just changes how the following string literal is interpreted. Backslashes (\) are not treated as escape sequences, that means that the regex in the string will be used as is.
^ at the beginning and $ at the end match and the end of the string respectively.
(?P<name>...) is a saving named group - it helps you to cut a part of url and pass it as a parameter into the view. See more in django named groups docs.
Hope that helps.