V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
leisurelylicht
V2EX  ›  Python

关于 django url 不匹配的问题

  •  
  •   leisurelylicht · 2017-04-04 16:16:07 +08:00 · 2023 次点击
    这是一个创建于 2795 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    2 条回复    2017-04-04 18:19:52 +08:00
    cocoakekeyu
        1
    cocoakekeyu  
       2017-04-04 17:52:58 +08:00
    正则表达式应该是`?P<name>`吧
    leisurelylicht
        2
    leisurelylicht  
    OP
       2017-04-04 18:19:52 +08:00
    @cocoakekeyu 对,我去吃了个饭回来就看到了,不知道为什么当时跟瞎了一样死活找不到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5056 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 09:46 · PVG 17:46 · LAX 01:46 · JFK 04:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.