url () to path()

51 views
Skip to first unread message

Kayode Oladipo

unread,
May 1, 2018, 1:57:03 AM5/1/18
to django...@googlegroups.com

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!

Mike Morris

unread,
May 1, 2018, 7:50:24 PM5/1/18
to django...@googlegroups.com

I'm not sure I understand the question, but here's my input:

--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Daniel Hepper

unread,
May 2, 2018, 3:36:21 AM5/2/18
to django...@googlegroups.com
Hi!

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

--
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.

Kayode Oladipo

unread,
May 2, 2018, 8:43:22 AM5/2/18
to django...@googlegroups.com

Thanks a Lot! That was very helpful

>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

> --
> 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...@googlegroups.com.


> To post to this group, send email to django...@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.

Reply all
Reply to author
Forward
0 new messages