Summary of the 10 DOS and don 'ts of the django 2.0 update

  • 2020-06-23 00:53:11
  • OfStack

preface

One of the biggest changes to the much-anticipated django 2.0 release is that it no longer supports es4EN2.x. This should be a wake-up call for those of us who still use x conservatively.

Python compatibility

Django 2.0 supports Python3.4, 3.5, and 3.6. The latest version of each series is highly recommended by Django.

Most importantly, Django 2.0 no longer supports Python2!

Django 1.11.ES27en is the last version that supports Python2.7.

django2.0 deprecation feature

Here are some of the most common mistakes I've encountered since upgrading to Django 2.0:

1. url


from django.core.urlresolvers import reverse

Turned out to be


from django.urls import reverse

2. MIDDLEWARE

settings.py file, MIDDLEWARE_CLASSES changed to MIDDLEWARE, this 1 must be noted.

3. django.shortcuts.render()

django.shortcuts.render_to_response() The method has been deprecated and is now in use django.shortcuts.render() Methods.

4. User. is_authenticated and User is_anonymous

Before the User.is_authenticated() and User.is_anonymous() Method changed to property: User.is_authenticated and User.is_anonymous .

5. SessionAuthenticationMiddleware

The SessionAuthenticationMiddleware class has been removed, the middleware is no longer needed and is turned on by default in Django 1.10+.

6. assignment_tag changed to simple_tag

@register.assignment_tag Changed to @register.simple_tag .

django2.0 updated features

Here are one of the new features encountered in the update to Django 2.0

7. on_delete=models.CASCADE

Attribute increase for ForeignKey and OneToOne for model on_delete=models.CASCADE .

8. URL preparation is simplified

Django the previous URL rule is the regular rule, written a bit anti-human, no point Pythonic. Developers 1 are forced to write matching expressions like this:


url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),

Now, you can write like this:


path('articles/<int:year>/', views.year_archive),

9. Aggregation operations

The aggregate operation annotate for database queries adds an operation called Window and a condition called Frame.

Home page 10.

In addition, the first launch of the welcome page has also been redone, feel a lot taller, there is no django2-ES131en

conclusion

For a complete list of the new features and changes to Django 2.0, see the official documentation.


Related articles: