为什么我在/获得NoReserverMatch?

时间:2020-08-15 14:53:39

标签: python django

我是Python的新手,正在尝试开发一个简单的博客应用程序。我在尝试规范网址时遇到NoReverseMatch /错误。尝试了不同的解决方案来解决此问题,但我正在获得旧版Django版本的帮助。请帮我提供更新的Django版本的解决方案。

错误: 找不到带有参数'(2020,'08','15','indian-software-industry')'的'post_details'。尝试了1个模式:['blog /(?P \ d {4})/(?P \ d {2})/(?P \ d {2})/(?P \ [-\ w] +)/ $']

这是我的urls.py

from django.contrib import admin
from django.urls import path,re_path
from blog import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.post_list_view),
    re_path(r'^blog/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>\[-\w]+)/$',
    views.post_detail_view,name='post_details'),

这是我的models.py(用于反向网址)

def __str__(self):
    return self.title
def get_absolute_url(self):
    return reverse('post_details',args=[self.publish.year,self.publish.strftime('%m'),
    self.publish.strftime('%d'),self.slug])

这是我的post_list.html

{%extends 'blog/base.html'%}
{%block title_block%} Sentamil's Blog Home Page{%endblock%}
  {%block content%}
  <h1>My Blog</h1>
  {%for post in post_list%}

  <a href="{{post.get_absolute_url}}"> <h2>{{post.title}}</h2></a>
  <p id='date'>Published on {{post.publish}} by {{post.author|title}}</p>
  {{post.body|truncatewords:30|linebreaks}}
  {%endfor%}
  {%endblock%}

2 个答案:

答案 0 :(得分:1)

这是路由URL的旧方法

re_path(r'^blog/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>\[-\w]+)/$',
    views.post_detail_view,name='post_details'),

在DJANGO 3.1中,此方法用于路由URL,就像我在下面所做的那样,这是可以理解的方法

re_path('<int:year>/<int:month>/<int:day>/<slug:post>/',views.post_detail_view,name='post_details'),

您必须像我在下面那样更改您的“规范URL”

def get_absolute_url(self):
 return reverse('blog:post_detail',
 args=[self.publish.year,
 self.publish.month,
self.publish.day, self.slug])

答案 1 :(得分:0)

您在最后一部分犯了一个错误,应该是这样的

select
    c.customer_id,
    p.product_id,
    sum(ii.qty) quantity
from customers c
cross join products p
left join invoices i 
    on  i.customer_id = c.customer_id
left join invoice_items ii
    on  ii.invoice_id = i.invoice_id
    and ii.product_id = p.product_id
group by c.customer_id, p.product_id