使用的环境:pycharm2018.1+Django2.1
第一章:
1.错误:author = models.ForeignKey(User, related_name='blog_posts')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
解决方案:author = models.ForeignKey(User, related_name='blog_posts', on_delete=models.CASCADE)
错误原因:指定外键的方式不对。
2.错误:blogf.Post: (models.E014) 'ordering' must be a tuple or list (even if you want to order by only one
field).
解决方案:将ordering的类型改为元组。
3.建立超级用户时,密码不能少于8位。不能过于简单。
4.get_object_or_404 方法需要引用
from django.shortcuts import get_object_or_404
5.ModuleNotFoundError: No module named 'django.core.urlresolvers'
错误原因:django2.0 把原来的 django.core.urlresolvers 包 更改为了 django.urls包,所以我们需要把导入的包都修改一下就可以了。
6.错误:
url(r'^blogf/', include('blogf.urls', namespace='blogf',app_name='blogf),
解决方案:参考https://www.cnblogs.com/wendaobiancheng/p/9109257.html
可写为:
url(r'^blogf/', include(('blogf.urls', 'blogf'), namespace='blogf')),
7.错误:不匹配错误:
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\ r'(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
解决方案: 在 django中推荐使用的是path,后改为了
path('<int:year>/<int:month>/<int:day>/<slug>/',
views.post_detail, name='post_detail'),
这样改完后,出现了一个无法解析的错误,原因出在时间解析上,最简单的方法是删除get_absolute()中时间的参数,去视图中也做相应修改。又或者是对时间参数进行重写。
8:django.urls.exceptions.NoReverseMatch: Reverse for ' post_detail' not found. ' post_detail' is not
a valid view function or pattern name.
解决方法:发现自己没有写错,通过查找发现
return reverse('blog:post_detail', args=[self.slug])
冒号后不能有空格
本人第一次写,如有不正确的地方,欢迎指正。