Regex for Django signed token - python

I have created a signed token using Django in order to create a URL for validating email addresses. I now need to ensure my urlpattern matches the token Django creates.
The token is created using the following function:
from django.core import signing
def create_token(verify_code, partial_token):
token = {
'verify_code': verify_code,
'partial_token': partial_token
}
return signing.dumps(token)
And I have the following url + regex:
url(r'^email/validation/(?P<signed_token>[^/]+)/$', core_views.email_validation,
name='email_validation')
Is (?P<signed_token>[^/]+) the correct regex to pickup all possible tokens created using that function.

The token generated by Django will be a base 64 string. You could find a regex to specifically match base 64, but this is unnecessary unless you have other URL patterns that could conflict with this one (and if you do, the better solution would be to modify your URL patterns).
The regex you currently have is simply catching all characters that aren't a /, which should work just fine.

Related

How to have exception for some words - Regex in django url pattern?

In my website, people can visit other's profile with url like https://example.com/slug-profile, here is the pattern:
path('<slug:slug>',views.profile,name='profile')
I also want that users go to settings,notifications:
path('settings',views.param,name='param')
# https://example.com/settings
path('notifications',views.notifications,name='notifications')
# https://example.com/notifications
But in this case, settings will be considered as a slug. I have already set a function to avoid users to have slug like settings or notifications.
I see that some websites use a prefix or something similar
www.example.com/user/settings/
www.twitter.com/i/notifications/
In my project I would do:
re_path(r'^(?P<slug>\w{5,})$',views.profile,name='profile')
path('i/settings',views.param,name='param')
i or user will never be considered as a slug profile since the slug regex expression must have at least 5 characters.
How to have thees words or a list of words ['settings','notifications'] as exceptions in a regex pattern?
Or
Do I need to go with example that contains i user in url?
The solution is simpler than this. Just put your specific URLs first; since patterns are always matched in order, settings and notifications will match their own paths and not the slug path.

Remove whitespaces from a url in Urlpatterns (django)

I'm new to Django and I'm developing a project in which there are profile pages.
Well, the problem is that the primary key has whitespaces but I don't want them to show in the url, neither like "%20%", I want to join the words. For example:
website.com/Example Studios --> website.com/examplestudios
I've tried this:
url ( (r'^(?P<studio_name>[\w ]+)/$').replace(" ", ""), views.StudioView, name = 'dev_profile')
But didn't work (it seems like it turns the raw part to string before reading the url) and with 're' happens the same. I've been searching for solutions but I'm not able to find them (and slugify doesn't convinces me).
What's solution to this or what do you recommend?
In urls you define patterens which Django will catch e.g. r'^test/$' this means that when someone try to get yourdomain.com/test Django will catch it and call the view which will probably render template. You need to solve your problem on url generation e.g. in template . Therefore in urls:
url ( (r'^(?P<studio_name>[\w ]+)/$'), views.StudioView, name = 'dev_profile').
You need to transform primary key to word without space in template. One way is to use slug for every record.
Also add slug field to Studio model which is autogenerated and non-editable field(you can generate it from name e.g. Example Studios->examplestudios).

django Url endswith regex in url path

I need to support following urls in single url regex.
/hotel_lists/view/
/photo_lists/view/
/review_lists/view/
how to support all above urls in single views?
I tried something like below
url(r'^\_lists$/(?P<resource>.*)/$', 'admin.views.customlist_handler'),
edit:
hotel,photo, review is just example. that first part will be dynamic. first part can be anything.
If you wish to capture the resource type in the view, you could do this:
url(r'^(?P<resource>hotel|photo|review)_lists/view/$', 'admin.views.customlist_handler'),
Or to make it more generic,
url(r'^(?P<resource>[a-z]+)_lists/view/$', 'admin.views.customlist_handler'), #Or whatever regex pattern is more appropriate
and in the view
def customlist_handler(request, resource):
#You have access to the resource type specified in the URL.
...
You can read more on named URL pattern groups here

Pass email as regex in django url

I am trying to pass email as parameter in django URL. I want to pass email as well as normal string and number also in URL as arguments.
url(r"search_connections/(?P<data>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$", "search_connections", name="search_connections"),
It's working properly for email as a parameter. But for normal string like "abc" it's not working:
working for "/search_connections/abc#test.com/"
not working for "/search_connections/abc/"
I want this URL to work for both.
You may try simply use | (or) with \w+:
r'search_connections/(?P<data>\w+|[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$'
However I think regex for email just isn't a robust solution to match all valid emails.

Python Django: urls.py questions

Need a little help with my urls.py and other stuff.
How can I replicate this in Django?
1) When user requests a non-existent page it will redirect to one up the directory level. Ex: example.com/somegoodpage/somebadpage should be redirected to example.com/somegoodpage.
2) When user requests page example.com/foo/bar/?name=John it will make url to example.com/foo/bar/name=John
3) When user requests page example.com/foo/bar/John it will change url to example.com/foo/bar/name=John.
Any help is greatly appreciated. Thank You.
For 1), if you don't want to do a separate route for every single route on your website, you'll need middleware that implements process_exception and outputs an HttpResponseRedirect.
For 2 and 3, those are rules that are presumably limited to specific routes, so you can do them without middleware.
2 might be doable in urls.py with a RedirectView, but since the relevant bit is a query string argument, I would probably make that an actual view function that looks at the query string. Putting a ? character in a url regex seems strange because it will interfere with any other use of query strings on that endpoint, among other reasons.
For 3, that's a straightforward RedirectView and you can do it entirely in urls.py.
according to django doc for number 1:
django URL dispatcher runs through each URL pattern, in order, and stops at the first one that matches the requested URL, so add a pattern that matches "somebadpage"s and assign it to a view which redirects the user to "somegoodpage".
for number 2:
the doc says "The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name."
so i don't think that you can get the "?name=John" in url dispather, so if you describe what you want to do maybe I can help better
and for 3:
to capture bits of the URL and pass them as positional arguments to a view you should use named regular-expression groups, for example :
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', 'news.views.month_archive'),
and the request to /articles/2005/03/ would call the function news.views.month_archive(request, year='2005', month='03'), instead of news.views.month_archive(request, '2005', '03').
hope this helped :)

Categories