【问题标题】:"NoReverseMatch" Reverse for 'by_rubric' with arguments '('',)' not found"NoReverseMatch" 未找到带有参数 '('',)' 的 'by_rubric' 的反向
【发布时间】:2021-03-01 01:27:27
【问题描述】:

我对 Python 和 Django 非常陌生。我试图通过练习工作制作代码,并且有一段时间很好,但是由于我尝试实现模板继承,所以我的主页上出现了这个错误。休息工作正常......现在。我试图在类似的主题和 django 文档中找到解决方案,但这对我没有帮助。请帮我解决这个问题,当我只有 1、2、3 时,导致第 0 行出现错误.. 真的很令人沮丧。由于我缺乏知识,即使是哪个文件导致此错误也很难理解。

错误:

NoReverseMatch at /bboard/
Reverse for 'by_rubric' with arguments '('',)' not found. 1 pattern(s) tried: ['bboard/(?P<rubric_id>[0-9]+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/bboard/
Django Version: 3.1.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'by_rubric' with arguments '('',)' not found. 1 pattern(s) tried: ['bboard/(?P<rubric_id>[0-9]+)/$']
Exception Location: D:\django\venv_1\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable:  D:\django\venv_1\Scripts\python.exe
Python Version: 3.8.5

我的意见.py:

from django.shortcuts import render
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy

from .models import Rubric
from .models import Bb
from .forms import BbForm


def index(request):
    bbs = Bb.objects.all()
    rubrics = Rubric.objects.all()
    context = {'bbs': bbs, 'rubrics': rubrics}
    return render(request, 'bboard/index.html', context)


def by_rubric(request, rubric_id):
    bbs = Bb.objects.filter(rubric=rubric_id)
    rubrics = Rubric.objects.all()
    current_rubric = Rubric.objects.get(pk=rubric_id)
    context = {'bbs': bbs, 'rubrics': rubrics, 'current_rubric': current_rubric}
    return render(request, 'bboard/by_rubric.html', context)


class BbCreateView(CreateView):
    template_name = 'bboard/create.html'
    form_class = BbForm
    success_url = reverse_lazy('index')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['rubrics'] = Rubric.objects.all()
        return context

我的 urls.py

from django.urls import path

from .views import BbCreateView
from .views import index, by_rubric

urlpatterns = [
    path('add', BbCreateView.as_view(), name='add'),
    path('<int:rubric_id>/', by_rubric, name='by_rubric'),
    path('', index, name='index'),
    ]

layout/basic.html 模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>{% block title %}Home{% endblock %} - Bill Board</title>
</head>
<body>
    <header>
        <h1>Advertisements</h1>
    </header>
    <nav>
        <a href="{% url 'index' %}">Home</a>
        <a href="{% url 'add' %}">Add New</a>
        {% for rubric in rubrics %}
        <a href="{% url 'by_rubric' rubric.pk %}">{{ rubric.name }}</a>
        {% endfor %}
    </nav>
    <section>
        {% block content %}
        {% endblock %}
    </section>
</body>
</html>

index.html

{% extends 'layout/basic.html' %}

{% block content %}
{% for bb in bbs %}
<div class="b">
    <h2>{{ bb.title }}</h2>
    <p>{{ bb.content }}</p>
    <p><a href="{% url 'by_rubric' rubric.pk %}">{{ rubric.name }}</a></p>
    <p>{{ bb.published|date:'d.m.Y H:i:s' }}</p>
</div>
{% endfor %}
{% endblock %}

by_rubric.html

{% extends 'layout/basic.html' %}

{% block title %}{{ current_rubric.name }}{% endblock %}

{% block content %}
<h2>Rubric: {{ current_rubric.name }}</h2>
{% for bb in bbs %}
<div>
    <h2>{{ bb.title }}</h2>
    <p>{{ bb.content }}</p>
    <p>{{ bb.published|date:'d.m.Y H:i:s' }}</p>
</div>
{% endfor %}
{% endblock %}

【问题讨论】:

  • &lt;p&gt;&lt;a href="{% url 'by_rubric' rubric.pk %}"&gt;{{ rubric.name }}&lt;/a&gt;&lt;/p&gt; 没有多大意义,因为您永远不会将变量 rubric 传递给 index.html

标签: python-3.x django django-templates


【解决方案1】:

这引发错误的原因是行:

<p><a href="{% url 'by_rubric' rubric.pk %}">{{ rubric.name }}</a></p>

index.html 模板中。您没有将rubric 对象传递给index.html 模板,因此rubric.pk 不存在,并被解析为空字符串,这与&lt;int:pk&gt; 路径转换器不匹配。

您可以将其替换为:

{% for rubric in rubrics %}
<p><a href="{% url 'by_rubric' rubric.pk %}">{{ rubric.name }}</a></p>
{% endfor %}

【讨论】:

  • 太棒了!!!!非常感谢!!! )))))我希望我能知道更多来自己解决这些问题,但是,我不会停止学习))。
猜你喜欢
  • 2013-07-25
  • 2018-09-08
  • 1970-01-01
  • 2016-11-11
  • 2018-06-28
  • 1970-01-01
  • 2021-08-11
  • 2021-11-09
  • 2013-12-22
相关资源
最近更新 更多