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>[^/]+)/?
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.
I can't match an email address with a trailing slash in my url regex, and I can't figure out why. Here's the regex that matches the email address with no trailing slash:
r'^customer/(?P<customer_email>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,63}$)$'
As expected, this matches /customer/someone#example.com, and not /customer/someone#example.com/
I would have thought that appending /? would work, given that the regex to match the domain suffix of the email address should not greedily match the slash. (This was the solution to many of the other duplicate regex trailing-slash questions).
r'^customer/(?P<customer_email>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,63}$)/?$'
As expected, this matches /customer/someone#example.com, but unexpectedly it doesn't match /customer/someone#example.com/. Why?
APPEND_SLASH in settings.py is not set. I don't want to capture the slash as part of the customer_email url parameter.
The $ anchor means the end of string, and the first time you have it inside a consuming pattern, it requires the end of string there.
Thus, you need to remove the first $ in your pattern and use
^customer/(?P<customer_email>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,63})/?$
See the regex demo.
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.
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.
i have this in my URL config:
url(r'^fotogalerie/(?P<nazev_slozky>([^/]+/)+)/$', fotogalerie_slozka),
I want to work for all URL that begin at fotogalerie, so for "fotogalerie/something/","fotogalerie/so/on/","fotogalerie/i/don't know/anything/". Why this isnt work?
You're requiring two trailing slashes, since your path subcomponents have to end with one in the subgroup definition and then you also have /$ at the end of your pattern. Depending on what your pattern should contain, either remove the final / or use a named group that has the final / optional.
That is, either:
url(r'^fotogalerie/(?P<nazev_slozky>([^/]+/)+)$', fotogalerie_slozka),
if you want your captured argument to be so/on/, or:
url(r'^fotogalerie/(?P<nazev_slozky>([^/]+/?)+)/$', fotogalerie_slozka),
if you want your captured argument to be so/on.