学习《Django By Example》的错误及解决方法(个人学习记录)

使用的环境: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])

冒号后不能有空格

本人第一次写,如有不正确的地方,欢迎指正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Key Features Learn Django by building real-world web applications from scratch Develop powerful web applications quickly using the best coding practices Integrate other technologies into your application with clear, step-by-step explanations and comprehensive example code Book Description If you want to learn the entire process of developing professional web applications with Django, then this book is for you. You will walk through the creation of four professional Django projects, teaching you how to solve common problems and implement best practices. You will learn how to build a blog application, a social image bookmarking website, an online shop and an e-learning platform. The book will teach you how to enhance your applications with AJAX, create RESTful APIs and setup a production environment for your Django projects. The book walks you through the creation of real-world applications, solving common problems, and implementing best practices. By the end of this book, you will have a deep understanding of Django and how to build advanced web applications. What you will learn Build practical real-world web applications with Django Use Django with other technologies such as Redis, Celery and Solr. Develop pluggable Django applications Create advanced features, optimize your code and use the cache framework Add internationalization to your Django projects Enhance the user experience using JavaScript and AJAX Add social features to your projects Build RESTful APIs for your applications Who This Book Is For If you are a web developer who wants to see how to build professional sites with Django, this book is for. You will need a basic knowledge of Python, HTML, and JavaScript, but you dont need to have worked with Django before. Table of Contents Chapter 1. Building a Blog Application Chapter 2. Enhancing Your Blog with Advanced Features Chapter 3. Extending Your Blog Application Chapter 4. Building a Social Website Chapter 5. Sharing Content in Your Website Chapter 6. Tracking User Actions Chapter 7. Building an Online Shop Chapter 8. Managing Payments and Orders Chapter 9. Extending Your Shop Chapter 10. Building an E-Learning Platform Chapter 11. Rendering and Caching Content Chapter 12. Building an API Chapter 13. Going Live
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值