DjangoUnleashed-2.0

51 views
Skip to first unread message

Midhat Šibonjić

unread,
Feb 20, 2018, 7:21:10 AM2/20/18
to Django users
Django 2.0 released before 2,5 months. We have some problems with code in Django Unleashed book. It's for Django 1.8, when someone want to write with Django 2.0 there are many problems with url section. Is there someone who would like to review the book and update code? Thanks.

Andy

unread,
Feb 20, 2018, 7:49:58 AM2/20/18
to Django users
How many problems are there and how big is this section?
There is quite a gap from 1.8 to 2.0 mainly the new locations of url functions which are nagging you.

How can i help?

Andrew Pinkham

unread,
Feb 23, 2018, 11:47:05 AM2/23/18
to django...@googlegroups.com
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



Reply all
Reply to author
Forward
0 new messages