Friday, February 23, 2018

Re: DjangoUnleashed-2.0

Hi,

I'm the author of Django Unleashed. A second edition is in the works, but it is taking some time (I'm also working on a video series).

Django 2.0 introduced a simplified system for URLs.

https://docs.djangoproject.com/en/2.0/releases/2.0/#simplified-url-routing-syntax

If you want wish to use the new syntax while following along with the examples in Django Unleashed, you have two choices.


Choice 1: replace regular expression syntax with the simplified syntax, using path matchers at the link below instead of regular expression patterns.

https://docs.djangoproject.com/en/2.0/topics/http/urls/#path-converters

In this case, the code below...

from django.conf.urls import url
...
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', viewCallable)

would be replaced with...

from django.urls import path
...
path('articles/<int:year>/<int:month>/<slug:slug>/', viewCallable)

Note that viewCallable is pseudocode for a function view or class-based view (viewClass.as_view()).

This involves changing every URL path (and now you know why the second edition is taking forever). Please note the new imports.


Choice 2: Use Django's new `re_path()` function instead of `url()` to fallback on original Django behavior.

In this case, the code below...

from django.conf.urls import url
...
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', viewCallable)

would be replaced with...

from django.urls import re_path
...
re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', viewCallable)

Only the function has changed: the regex pattern has not.

Read more about that at the link below.

https://docs.djangoproject.com/en/2.0/topics/http/urls/#using-regular-expressions


I might recommend using Choice 2 when following along with the book, but to give Choice 1 a whirl (after reading Django's documentation) on a new project as it's easier to read.

I hope this helps!

Andrew
http://jambonsw.com
http://django-unleashed.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+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/52151FC1-6C32-4D85-9C2C-199BC849CCB8%40andrewsforge.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment