【问题标题】:NoReverseMatch at /polls/2/ [closed]/polls/2/ 的 NoReverseMatch [关闭]
【发布时间】:2020-07-13 23:43:16
【问题描述】:

Django 版本: 3.0.8

Python 版本: 3.8.0

我正在做一个 Django 教程,我在下面遇到了这个错误

下面的“detail.html”

{% extends 'polls/base.html' %}

{% block main_content %}
<h1>{{question.question_text}}</h1>

{% if error_message %}<p><strong>{{error_message}}</strong></p>{% endif %}

<form action ="{% url 'polls:vote' question.id %}" method="post">
   {% csrf_token %}
   {% for choice in question.choice_set.all %}
     <input type="radio" name = "choice" id="choice{{forloop.counter}}" value="{{choice.id}}"/>
     <label for="choice{{forloop.counter}}">{{choice.choice_text}}</label><br>
   {% endfor %}

<input type="submit" value="vote"/>
</form>

{% endblock %}

下面的“base.html”

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Base</title>
</head>
<body>

<hr>
<p> Header </p>
<hr>
{% block main_content %}
{% endblock %}
{% include "polls/footer.html" %}
</body>
</html>

如果有人能提供帮助,我将不胜感激。

【问题讨论】:

  • 请同时添加footer.html的代码
  • 向我们展示你的 urls.py
  • 'from django.conf.urls import * from .导入视图 urlpatterns = \ [ url(r'^$', views.index, name="index"), url(r'^(?P[0-9]+)/$', views.detail , name="detail"), url(r'^(?P[0-9]+)/results$', views.results, name="results"), url(r'^(?P [0-9]+)/votes$', views.votes, name="votes"), ] app_name = 'polls' '

  • 页脚


  • 这是一个错字,你的路线反向名称=投票不投票,你应该相应地编辑你的模板

标签: python-3.x django


【解决方案1】:

主要问题在于您在评论中分享的urls.py。请将最后一个 URL 名称从 votes 替换为 vote。希望它能解决这个错误。请测试并分享您的结果。谢谢

解决方案代码:

from django.conf.urls import * 
from . import views 

urlpatterns = [ 
url(r'^$', views.index, name="index"), 
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name="detail"),
url(r'^(?P<question_id>[0-9]+)/results$', views.results, name="results"),
url(r'^(?P<question_id>[0-9]+)/vote$', views.votes, name="vote"), 
] 
app_name = 'polls'

【讨论】:

    【解决方案2】:

    您的polls/footer.html,在某处使用{% url 'vote' ... %} 而不是{% url 'polls:vote' ... %}

    【讨论】:

      【解决方案3】:

      您可能忘记在urls.py 中注册投票路径,例如path('&lt;int:pk&gt;/vote/', views.vote, name='vote'), 您还需要在views.py 中使用视图处理程序,它是用作views.vote 的视图处理程序

      【讨论】:

        猜你喜欢
        • 2018-02-03
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 2018-02-07
        • 2016-12-24
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多