Django 2.0升级小记

Python 2018-01-06 6250
预计阅读时间:2 分钟

备受期待的django 2.0已经发布了,最大的一个变化就是不再支持python2.x版本了,另外还有在使用方面有了一些变化,下面就介绍下我在升级的时候遇到的错误。

from django.core.urlresolvers import reverse

变成了

from django.urls import reverse
  • settings.py文件,MIDDLEWARE_CLASSES更改成了MIDDLEWARE

  • django.shortcuts.render_to_response()方法已经被弃用了,现在使用django.shortcuts.render()方法。

  • User.is_authenticated()User.is_anonymous()方法更改成了属性:User.is_authenticatedUser.is_anonymous
  • 删除了SessionAuthenticationMiddleware类,不再需要该中间件,在Django 1.10+中已经默认开启了。
  • @register.assignment_tag改成了@register.simple_tag
  • 为model的ForeignKeyOneToOne的属性增加on_delete=models.CASCADE
  • url更简单了,如下:
path(r'page/<int:page>/',views.IndexView.as_view(), name='index_page'),
path(r'article/<int:year>/<int:month>/<int:day>/<int:article_id>.html',views.ArticleDetailView.as_view(),name='detailbyid')
path(r'category/<slug:category_name>.html', views.CategoryDetailView.as_view(), name='category_detail')

另外如果使用include('comments.urls',namespace='comment')这种方式来引用url文件的话,需要在url文件中加上app_name,如: app_name = "comments"

  • AbstractUser.last_name的最大长度增加到150 如果你有一个自定义的用户模型继承了AbstractUser,你需要生成并应用一个数据库迁移,使得last_name的最大长度变为150。

完整的更新变化请参考:官方文档


本文由 liangliangyy 原创,转载请注明出处。

相关推荐

评论

9
  1. 峰影

    真正参考你的代码设计进行学习中,学的差不多了也用Django做个博客。

  2. DieYoungWsn

    请问这个博客系统普通用户是不是发布不了博客,只能看?

  3. 18080954900

    学习了

发表评论

登录 后发表评论

发现更多