Hi!
int [0-9]+
str [^/]+
path .+
slug [-a-zA-Z0-9_]+
uuid [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
First of all, you still can use regular expressions to define URLs by using django.urls.re_path. You could even the old django.conf.urls.url, which is still available, so you don't have to change any existing code.
What you are asking for is the corresponding path converter to a regular expression.
Django comes with five predefined path converters, which just use a regular expression under the hood:
int [0-9]+
str [^/]+
path .+
slug [-a-zA-Z0-9_]+
uuid [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
Note that those also do type conversion when appropriate, the int path converter passes an int to your view, the uuid converter a uuid.
If those don't fit your needs, you can also build your own:
Or, as mentioned at the beginning, just keep using regular expressions:
I've just put together a small article that sums up everything: https://consideratecode.com/2018/05/02/django-2-0-url-to-path-cheatsheet/
Hope that helps,
Daniel
On Tue, May 1, 2018 at 7:53 AM, Kayode Oladipo <oladipokayode0@gmail.com> wrote:
Hi there again! Beginner here using Django 2.0.
I'm following a tutorial (coupled with the official doc for compatibility) and I discovered that <2.0 versions use RegEx with the url but 2.0> uses the simplified path() function.
My question is how do I find the right corresponding path() function to a url (),
A cheat sheet would be wonderful.Cheers!
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com .
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users .
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA% .2BARzD8HO26aTwpdYe1km9_% 3DykDgBF_y% 3DRfkNhrpFvHmSsea6w%40mail. gmail.com
For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHEnUVVsYBhwCsJQ1fkf1eb-6EwHMVQKbTH_eEtVbJ4-roLKVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment