错误:
NoReverseMatch at /blog/
Reverse for 'blog.views.blogpost' not found. 'blog.views.blogpost' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.11.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'blog.views.blogpost' not found. 'blog.views.blogpost' is not a valid view function or pattern name.
解决措施:
reverse()这又是啥?Django官方文档之Reverse
return reverse('blog.views.blogpost',
kwargs={'slug': self.slug, 'post_id': self.id})
reverse()介绍,reverse()用于产生url的最终形式。例如我点开文章,他会生成一大串一大串的 url地址。 类似这样的,http:/blog.csdn.net/weixin_36571185/article/details/73715296。
生成的url最终去了哪里呢?
#这里面我用到了url反向解析。
def get_absolute_url(self):
return reverse(blog.views.blogpost,
kwargs={'slug': self.slug, 'post_id': self.id})
最终,他出现在这里》》》》
reverse()用法:
model.py
def get_absolute_url(self):
return reverse('blogposts'# 要么用昵称blogposts,要么用视图全称
kwargs={'slug': self.slug, 'post_id': self.id})
def get_absolute_url(self):
return reverse(blog.views.blogpost,#这是视图全称。
kwargs={'slug': self.slug, 'post_id': self.id})
url.py
url(r'^(?P<slug>[-\w\d]+),(?P<post_id>\d+)/$'views.blogpost,name='blogposts'),
Blog.objects.exclude()是个什么玩意呢?
exclude()是过滤器,相当于sql语句里的where。用来做条件判断的。初次之外还有all(),fillter()。他们返回的都是一个查询集Queryset
filter(**kwargs)
Returns a new QuerySet containing objects that match the given lookup parameters.exclude(**kwargs)
Returns a new QuerySet containing objects that do not match the given lookupparameters. Thelookup parameters (**kwargs in the above function definitions) should be in the format described in Field lookups below.