请问这个错误是哪里出了问题?
这是我的 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 的代码写的为什么会有问题呢?
1
cocoakekeyu 2017-04-04 17:52:58 +08:00
正则表达式应该是`?P<name>`吧
|
2
leisurelylicht OP @cocoakekeyu 对,我去吃了个饭回来就看到了,不知道为什么当时跟瞎了一样死活找不到
|