【问题标题】:NoReverseMatch at /blog/ in django 2.0django 2.0 中 /blog/ 的 NoReverseMatch
【发布时间】:2018-11-19 06:00:33
【问题描述】:

在尝试设置博客类别页面时遇到此错误 /blog/ 上的 NoReverseMatch 未找到带有参数 '('',)' 的 'category_detail' 的反向操作。尝试了 1 种模式:['blog\/category\-detail\/(?P[-a-zA-Z0-9_]+)$']

这是我的 url.py

from django.urls import path,include
from .import views

urlpatterns = [
    path('blog/',views.post_list,name="post_list"),
    path('blog/post-detail/<slug:slug>',views.post_detail,name="post_detail"),
    path('blog/category-detail/<slug:slug>',views.category_detail,name="category_detail"),

]

views.py

from django.shortcuts import render,get_object_or_404
from.models import Post,Category

# Create your views here.
def post_list(request):
    object_list=Post.objects.all()
    context={
        'object_list': object_list,
    }
    return render(request,"blog.html",context)

def post_detail(request,slug=None):
    post=get_object_or_404(Post,slug=slug)
    context={
       'post':post,
    }
    return render(request,"post_detail.html",context)

def category_detail(request,slug=None):
    category=get_object_or_404(Category,slug=slug)
    post=Post.objects.filter(category=category,status='Published')
    context={
        'category':category,
        'post':post,
    }
    return render(request,"category_detail.html",context)

blog.html

{% for obj in object_list %}
    {% if obj.status == 'Published' %}
      <article>
        <div class="embed-responsive embed-responsive-16by9">
      <img src="images/blog1.jpg" alt="" />
      </div>
        <div class="post-content">

    <h2>{{obj.title}}</h2>

    <div>
    {{obj.created}}  Author {{obj.user}} <h4><a href="{% url 'category_detail' slug=post.category.slug %}">{{obj.Category}}</a></h4>
    <hr/>
    <p>{{obj.body}}</p>
    <a class="mtr-btn button-navy ripple" href= "{% url 'post_detail' obj.slug %}">Continue reading →</a><br>
    </div>
    </article>
    {% endif %}
{% endfor %}

category_detail.html

{% extends "base.html" %}
{% load static %}
{% block seo_title %}{{category.seo_title}}{% endblock %}
{% block seo_description %}{{category.seo_description}}{% endblock %}
{% block Content %}
<h2>{{category.title}}</h2>
<p>{{category.description}}</p>
{% for item in post %}
{{item.title}}
{{item.body|truncatechars:50}}
{% endfor %}
{% endblock Content %}

注意其他 VIEWS.PY 工作正常,只是 category_detail 函数

【问题讨论】:

  • 当您引用 obj 时,您应该将您的 url 更改为 {% url 'category-detail' obj.category.slug %}

标签: javascript python html css django


【解决方案1】:

正如错误所说,这里缺少参数。也许您需要将{% url 'category_detail' slug=post.Category.slug %} 更改为{% url 'category_detail' slug=obj.category.slug %},因为我在blog.html 模板中看不到任何post 变量引用。

更新

您尚未共享您的模型代码,但我假设您的 Post 模型具有 Category 模型的外键,它看起来像 Category=models.ForeignKey(Category)。所以你需要像这样更新视图:

def category_detail(request,slug=None):
    category=get_object_or_404(Category,slug=slug)
    post=Post.objects.filter(Category=category,status='Published')

【讨论】:

  • 感谢工作,但转到 url localhost:8000/blog/category-detail/slug 会在 /blog/category-detail/python 处出现错误 FieldError 无法将关键字“类别”解析到字段中。选项有:Category, Category_id, body, created, id, seo_description, seo_title, slug, status, title, updated, user, user_id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 2017-11-26
  • 1970-01-01
  • 2018-10-27
  • 2017-09-24
相关资源
最近更新 更多