Django url pattern to ignore extra characters - django

I have a django url shown below :
url(r'^update_status/field1/(?P<field1_id>.*)/field2/(?P<field2_id>.*)/$', 'update_status', name='update_status')
This catches both the urls like :
update_status/field1/0445df4d8e1c43ae9/field2/f12b6b5c98/mraid.js/
and
update_status/field1/0445df4d8e1c43ae9/field2/f12b6b5c98
I want to capture only the second url . What should be changed in the django url?

Your regular expression (?P<field_id>.*) catches any character, including / characters. You want to restrict it to the format of the field_ids like this: (?P<field1_id>[0-9a-f]+) (same for field2_id).
Note: I'm assuming your id consists only of hexadecimal characters.

Related

Match url with uppercase letters except if it contains a filename like .jpg,.css,.js etc

I need a Regular Expression that can match url with uppercase letters but do not match if it contains a filename like .jpg,.css,.js etc
I want to redirect all uppercase url to lowercase but only when it is not pointing to a file resource.
Try using a regex visualizer like regexpal.com.
Here's an example of a regular expression that approximates what you're trying to do:
\w+\.(?:com|net)(?:/[A-Z]+){1,}[/]?(?:\.jpg|\.png|\.JPG|\.PNG){0}$
\w+\.(?:com|net) captures a domain of the form word.com or word.net. (You'll need to add other domains or improve this if you want to capture subdomains as well.)
(?:/[A-Z]+){1,}[/]?captures all-caps directories like /FOO/BAR/ with an optional trailing slash.
(?:\.jpg|\.png|\.JPG|\.PNG){0}$ captures exactly zero of the extensions listed; you'll obviously need to add to this list of extensions.
But perhaps rethink your routing; it's better form to keep all assets in devoted directories on your server, so that you can simply pass any request to mysite.com/assets/ along unchanged while handling other URLs.

Django Url pattern regex for tokens

I need to pass tokens like b'//x0eaa#abc.com//x00//xf0//x7f//xff//xff//xfd//x00' in my Django Url pattern. I am not able to find matching regex for that resulting Page not found error.
My url will be like /api/users/0/"b'//x0eaa#abc.com//x00//xf0//x7f//xff//xff//xfd//x00'"/
I have tried with following regex
url(r'^api/users/(?P<username>[\w\-]+)/(?P<paging_state>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.getUserPagination),
Please pass the token in request header or body and then use accordingly in your view.
Considering there are some static predictable elements in your url like -
api/users/
/" before b
"/ at the end after '
So I can see the url in either of the 2 ways below. Regex's mentioned accordingly:
api/users/(set of words, digits or hyphens)/"(any character except newline)"/
REGEX: ^api\/users\/([\w\d\-]+)\/"(.*)"\/$
URL: url(r'^api\/users\/([\w\d\-]+)\/"(.*)"\/$', views.getUserPagination),
api/users/(set of words, digits or hyphens)/"(one character-b)'//(any no. of words or digits)#(any no. of words or digits).(any no. of words or digits) (any no. of words, digits, front slashes)'"/
REGEX: ^api\/users\/([\w\d\-]+)\/"([a-g]'\/\/[\w\d]*#[\w\d]*.[\w\d]*[\/\w\d]*')"\/$
URL: url(r'^api\/users\/([\w\d\-]+)\/"([a-g]'\/\/[\w\d]*#[\w\d]*.[\w\d]*[\/\w\d]*')"\/$', views.getUserPagination),
You should be able to use either of the above two. There can be multiple ways to match the token part in your url. So unless it is a big security concern, you can do with the simplest approach as mentioned in point 1.

nginx regex - match variable number of fields

I have a route with urls that can have an optional extra field. It can be either of the form :
"/my-route/azezaezaeazeaze.123x456.jpg"
"/my-route/azezaezaeazeaze.123x456.6786786786.jpg"
with :
"azezaezaeazeaze" being a mongoId
123x456 two integers separated by "x"
6786786786 a unix timestamp
jpg an image extension (could be jpeg, png, gif...)
all those are separated by a "."
I would like to remove the optional part (the timestamp) from the request with the http rewrite module. So that the second url effectively becomes lie the first.
I made a small test on regex101 to get the groups, but :
- it doesn't seem to be the right syntax for nginx
- I do not see how it will allow me to remove the timestamp
How can I remove the timestamp from that url?
Starting from the right-hand end, you need to match a dot followed by anything
except a dot, so we have (\.[^.]*)$, then moving to the left, we want
to match a dot followed by only digits \.[0-9]*, which we dont want to
capture, and then to the left of that we want everything.
I ended up with something like this:
rewrite ^(.*)\.[0-9]*(\.[^.]*)$ $1$2 ;
Capitalizing on my first attempt and #meuh answer, I ended up with the following :
rewrite ^(/.*\..*)(\..*)(\..*)$ $1$3 last;
Now it works, but I would welcome any comment regarding the style/efficiency of this rewrite.

how to get value of url parameter with colon in text in django

I have the following url:
http://mysite.com/config/1:1/
This gives me a 404 page not found.
If I try this: it find my url entry no problem.
http://mysite.com/config/1/
This what my url pattern looks like:
url(r'^config/(?P<config_id>\d+)/$', views.config, name='config'),
Is there a problem with having the colon in the url?
Your regular expression allows only digits. \d doesn't match :. If your view function is able to handle the colon, broaden the pattern:
url(r'^config/(?P<config_id>[\d:]+)/$', views.config, name='config')
Since you've named the pattern config_id I suspect it's a primary key, and this will not work with the view, but that depends on the view itself.

capturing object id from a url in django view not in urls.py?

I know django.conf.urls.defaults.url extracts regular expression to get object-id or others.
I wonder if I can do the same thing in my view.
Suppose I have a url rule such as url(r'^/path1/path2/(?P<my_object_id>\d+)/path3, my_view, name='my_view_name')
In a view, I have a string that matches the url such as /path1/path2/34/path3, how can I get the 34 from the string?
ie, Is there a function which takes view_name(or the same url regex in urls.py), and a url string and returns positional/keyword arguments as the url() does it?
foo(view_name, url_string):
...
return (args, kwargs)
You can use django.core.urlresolvers.resolve.
You should be able to reference 34 using my_object_id.
Here's the relevant text from the django tutorial polls app:
The poll_id='34' part comes from (?P\d+). Using parentheses around a pattern
“captures” the text matched by that pattern and sends it as an argument to the view
function; ?P defines the name that will be used to identify the matched pattern;
and \d+ is a regular expression to match a sequence of digits (i.e., a number).