Python 添加迁移后模板将不起作用

Python 添加迁移后模板将不起作用,python,django,python-3.x,django-templates,django-migrations,Python,Django,Python 3.x,Django Templates,Django Migrations,我通过迁移添加了数据,现在运行了migrate和makemigrations 我试图运行服务器,但到处都没有EverSematch错误 请查看此错误: NoReverseMatch at /blog/ Reverse for 'blog_post_detail' with keyword arguments '{'year': 2008, 'month': 9, 'slug': 'django-10-released'}' not found. 1 pattern(s) tried: ['(?

我通过迁移添加了数据,现在运行了migrate和makemigrations 我试图运行服务器,但到处都没有EverSematch错误

请查看此错误:

NoReverseMatch at /blog/

Reverse for 'blog_post_detail' with keyword
arguments '{'year': 2008, 'month': 9, 'slug': 'django-10-released'}'
not found. 1 pattern(s) tried:
['(?P<year>\\d{4}/)^(?P<month>\\d{1,2}/)^(?P<slug>\\w+)/$']
这是实际的URL模式:

    re_path (r'^(?P<year>\d{4}/)'
        r'^(?P<month>\d{1,2}/)'
        r'^(?P<slug>\w+)/$',post_detail,name='blog_post_detail'),
同样,每个模板都有相同的问题

^匹配字符串的起始,因此不应该将它包含在正则表达式的中间。将其从月份和段塞字符串中移除。还应将前斜杠移到命名组之外。如果slug包含连字符,则需要使用[\w-]+而不是\w+

就我个人而言,当这个正则表达式被拆分成多行时,我发现它更难。我希望:

re_path (r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<slug>[\w-]+)/$',
         post_detail,name='blog_post_detail'),

我不明白你的评论。我的回答可能无法解决您的问题,但至少会给您一个不同的错误消息。除非我知道您正在运行的代码和收到的错误消息,否则我无法帮助您。您问题中的代码仍然有^s和/s位于错误的位置。如果您使用我的答案,那么您可能仍然会得到NoReversematch,但确切的错误消息文本将不同。我帮不上忙,因为我不知道确切的代码和错误消息。从数据库中删除所有数据。如果数据库中没有任何项,则用于反转URL的代码不会运行,因此不会出现错误。正如文档所建议的,您可能需要删除数据库并运行migrate,这将重新运行所有迁移。如果您使用的是SQLite,您可以删除db.sqlite3文件。我注意到正则表达式中的另一个问题-它不能处理slug中的连字符。请参阅上面更新的答案。请不要使用>缩进代码或错误消息。它隐藏了尖括号,因此显示的是['?P\d{4}/^?P\d{1,2}/^?P\w+/$'],而不是['?P\\d{4}/^?P\\d{1,2}/^?P\\w+/$']。
re_path (r'^(?P<year>\d{4})/'
    r'(?P<month>\d{1,2})/'
    r'(?P<slug>[\w-]+)/$',post_detail,name='blog_post_detail'),
re_path (r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<slug>[\w-]+)/$',
         post_detail,name='blog_post_detail'),