关于 django url 不匹配的问题

2017-04-04 16:16:07 +08:00
 leisurelylicht

请问这个错误是哪里出了问题?

这是我的 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 的代码写的为什么会有问题呢?

1951 次点击
所在节点    Python
2 条回复
cocoakekeyu
2017-04-04 17:52:58 +08:00
正则表达式应该是`?P<name>`吧
leisurelylicht
2017-04-04 18:19:52 +08:00
@cocoakekeyu 对,我去吃了个饭回来就看到了,不知道为什么当时跟瞎了一样死活找不到

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/352450

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX