请问这个错误是哪里出了问题?
这是我的 url
urlpatterns = [
#post views
url(r'^$', views.post_list, name='post_list'),
url(r'^(P?<year>\d{4})/(P?<month>\d{2})/(P?<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
]
models
@python_2_unicode_compatible
class Post(models.Model):
STATUS_CHOICES = (
('draft', '草稿'),
('published', '发布')
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES,default='draft', verbose_name='状态')
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
错误信息是
NoReverseMatch at /blog/
Reverse for 'post_detail' with arguments '(2017, '04', '04', 'test1')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(P?<year>\\d{4})/(P?<month>\\d{2})/(P?<day>\\d{2})/(?P<post>[-\\w]+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.10.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post_detail' with arguments '(2017, '04', '04', 'test1')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(P?<year>\\d{4})/(P?<month>\\d{2})/(P?<day>\\d{2})/(?P<post>[-\\w]+)/$']
Exception Location: /usr/local/lib/python3.6/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 392
Python Executable: /usr/local/opt/python3/bin/python3.6
Python Version: 3.6.1
Python Path:
['/Users/Licht/Code/Python/Django_by_Example/mysite',
'/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages']
Server time: 星期二, 4 四月 2017 16:08:35 +0800
我是照着 django by example 的代码写的为什么会有问题呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.