Python Django:使用参数'('',)'和关键字参数'{}'未找到'detail'的反转

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19336076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:29:54  来源:igfitidea点击:

Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found

pythondjangodjango-templatesdjango-views

提问by fromPythonImportNoob

I'm following the official tutorial to learn Django and using 1.5.

我正在按照官方教程学习 Django 并使用 1.5。

I had this link as part of my index template, which was working fine:

我将此链接作为索引模板的一部分,它运行良好:

<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>

however, this is hardcoded and the tutorial suggested a better way was to use:

然而,这是硬编码的,教程建议更好的方法是使用:

<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>

so that you'll be better of when dealing with huge number of templates and u have to make changes to the url.

这样你在处理大量模板时会更好,你必须对 url 进行更改。

Since I made the above change I get the following errors when I run the app:

由于我进行了上述更改,因此在运行应用程序时出现以下错误:

Exception Type: NoReverseMatch
Exception Value:    Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found.

My urls.py looks like this:

我的 urls.py 看起来像这样:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
   url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),                     
)

views.py looks like this:

views.py 看起来像这样:

from django.shortcuts import render, get_object_or_404
from django.http import Http404

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    context = {'latest_poll_list': latest_poll_list}
    return render(request, 'polls/index.html', context)


def detail(request, poll_id):
    poll = get_object_or_404(Poll, pk = poll_id)
    return render(request, 'polls/detail.html', {'poll': poll})

my index.html template looks like this:

我的 index.html 模板如下所示:

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="{% url 'polls:detail' poll_id %}">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p> No polls are available.</p>
{% endif %}

Usually I could easily read where the error is coming from and deal with it but in this case I can't spot the cause of the error hence I'm unable to progress with my study. Any help will be greatly appreciated.

通常我可以轻松地读取错误的来源并进行处理,但在这种情况下,我无法发现错误的原因,因此我无法继续学习。任何帮助将不胜感激。

采纳答案by Thijs van Dien

In your index.htmlyou gave poll_idas an argument, but that's just the name the argument will have within the detailfunction; it is not defined in your template. The actual value you want to call the function with is probably poll.id.

在你的参数中,index.html你给出了poll_id一个参数,但这只是参数在detail函数中的名称;它没有在您的模板中定义。您想要调用函数的实际值可能是poll.id.

回答by sam

I struggled with this for a while. Then I noticed I had put poll.id and not Poll.id with a (capital P)

我为此挣扎了一段时间。然后我注意到我把 poll.id 而不是 Poll.id 用(大写 P)

回答by Vanuan

This happened to me when I was reading tutorial. I didn't change poll_id to pk:

这在我阅读教程时发生在我身上。我没有将 poll_id 更改为 pk:

url(r'^(?P<poll_id>\d+)/$', views.DetailView.as_view(), name='detail'),

vs

对比

url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),

回答by dmvianna

My mistake was a typo on detail.html:

我的错误是打错了detail.html

<form action={% url 'polls:vote' polls.id %}" method="post">

should have been

本来应该

<form action={% url 'polls:vote' poll.id %}" method="post">

It took a while for me to realise the django traceback pagewas pointing me to the relevant line of code the whole time. :$

我花了一段时间才意识到django 回溯页面一直将我指向相关的代码行。:$

回答by VeganxEdge

also, in

还有,在

polls/urls.py

民意调查/网址.py

i had spelling error

我有拼写错误

url(r'^(?P[0-9]+)/$', views.detail, name='details'),

url(r'^(?P[0-9]+)/$', views.detail, name='detail s'),

vs the correct code

vs正确的代码

url(r'^(?P[0-9]+)/$', views.detail, name='detail'),

url(r'^(?P[0-9]+)/$', views.detail, name='detail'),

spent some time looking for the error, so look for proper spelling. lol

花了一些时间寻找错误,因此请寻找正确的拼写。哈哈

回答by Sharath K P

The error got sorted out for me after correcting the filter condition in views.py.

更正views.py 中的过滤条件后,该错误已为我解决。

snippet of my views.py

我的views.py的片段

def post_share(request, post_id):
        post = get_object_or_404(Post, id=post_id, status='Published')

snippet from my models.py

我的models.py中的片段

class Post(models.Model):
STATUS_CHOICES=(
                ('draft','Draft'),
                ('published','Published'),
                )

1st value is stored in the database and the second value is for displaying to the users.

第一个值存储在数据库中,第二个值用于向用户显示。

raw data from my mysql DB

来自我的 mysql 数据库的原始数据

+---------------------------------------+-----------+
| title                                 | status    |
+---------------------------------------+-----------+
| Revolution 2020                       | published |
| harry potter and the sorcerer's stone | published |
| harry potter and the cursed child     | draft     |
| five point someone                    | published |
| half girlfriend                       | draft     |
| one night at the call center          | published |
| Django by example                     | published |
+---------------------------------------+-----------+

When I had used "published", I was getting the said error. Once I changed the filter to "Published" it all sorted out.

当我使用“已发布”时,我收到了上述错误。一旦我将过滤器更改为“已发布”,一切就都解决了。