Regex URL in django doesnt work - python

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.

Related

Why don't include url regexes in django check for end of string

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.

How can I find all Markdown links using regular expressions?

In Markdown there is two ways to place a link, one is to just type the raw link in, like: http://example.com, the other is to use the ()[] syntax: (Stack Overflow)[http://example.com ].
I'm trying to write a regular expression that can match both of these, and, if it's the second match to also capture the display string.
So far I have this:
(?P<href>http://(?:www\.)?\S+.com)|(?<=\((.*)\)\[)((?P=href))(?=\])
Debuggex Demo
But this doesn't seem to match either of my two test cases in Debuggex:
http://example.com
(Example)[http://example.com]
Really not sure why the first one isn't matched at the very least, is it something to do with my use of the named group? Which, if possible I'd like to keep using because this is a simplified expression to match the link and in the real example it is too long for me to feel comfortable duplicating it in two different places in the same pattern.
What am I doing wrong? Or is this not doable at all?
EDIT: I'm doing this in Python so will be using their regex engine.
The reason your pattern doesn't work is here: (?<=\((.*)\)\[) since the re module of Python doesn't allow variable length lookbehind.
You can obtain what you want in a more handy way using the new regex module of Python (since the re module has few features in comparison).
Example: (?|(?<txt>(?<url>(?:ht|f)tps?://\S+(?<=\P{P})))|\(([^)]+)\)\[(\g<url>)\])
An online demo
pattern details:
(?| # open a branch reset group
# first case there is only the url
(?<txt> # in this case, the text and the url
(?<url> # are the same
(?:ht|f)tps?://\S+(?<=\P{P})
)
)
| # OR
# the (text)[url] format
\( ([^)]+) \) # this group will be named "txt" too
\[ (\g<url>) \] # this one "url"
)
This pattern uses the branch reset feature (?|...|...|...) that allows to preserve capturing groups names (or numbers) in an alternation. In the pattern, since the ?<txt> group is opened at first in the first member of the alternation, the first group in the second member will have the same name automatically. The same for the ?<url> group.
\g<url> is a reference to the named subpattern ?<url> (like an alias, in this way, no need to rewrite it in the second member.)
(?<=\P{P}) checks if the last character of the url is not a punctuation character (useful to avoid the closing square bracket for example). (I'm not sure of the syntax, it may be \P{Punct})

Django URLs - trailing slash gets added to variable value

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>[^/]+)/?

Django: Can't resolve an URL pattern from urls.py

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.

Django URL regex to accept no number or any number till 99

I have a url like this
test/report
and
test/report/1
and i want to write a matching url regex using django
I tried out this
url(r'^test/report/(0?\d+)/$'
but it did not work . Can any one know how to do this? Thank you in advance.
you just need to create two regexes, one without the slash, and another including the slash:
url(r'^test/report$'
url(r'^test/report/(\d\d?)$
lenik's answer is generally the proper way, but if you must have it in a single regex, you should be able to use this:
url(r'^test/report(/\d\d?)?$')
# Meaning:
# ( Start contents of a node
# / A slash
# \d A digit
# \d? A second digit, or nothing
# )? Finish node. Must find everything in the node, or nothing
Can you please try with url(r'^test/report/\d{1,2}/$' instead ?

Categories

Resources