【问题标题】:Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not foundDjango:使用参数'('',)'和关键字参数'{}'反转'详细信息'
【发布时间】:2013-10-20 14:13:15
【问题描述】:

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

我将此链接作为我的索引模板的一部分,它工作正常:

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

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

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

这样您在处理大量模板时会变得更好,并且您必须对 url 进行更改。

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

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

我的 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 看起来像这样:

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})

我的 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 %}

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

【问题讨论】:

  • 我注意到的一件事是你在index.html 中使用了命名空间表示法(polls:detail),但没有在urlpatterns 中将polls 定义为命名空间。试着让它只是detail 没有polls: 片刻。 poll_id 不应该是poll.id 吗?
  • 非常感谢.. 问题出在 poll_id 而不是 poll.id 上。现在可以了。你能把它作为答案让我接受吗?再次感谢

标签: python django django-templates django-views


【解决方案1】:

在您的index.html 中,您将poll_id 作为参数提供,但这只是detail 函数中参数的名称;它没有在您的模板中定义。您要调用函数的实际值可能是poll.id

【讨论】:

    【解决方案2】:

    我的错误是detail.html 的拼写错误:

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

    应该是

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

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

    【讨论】:

      【解决方案3】:

      当我阅读教程时,这发生在我身上。我没有把 poll_id 改成 pk:

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

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

      【讨论】:

        【解决方案4】:

        当我使用字符串作为原始值而不是用引号括起来时遇到此错误,即

        {% url 'my_view' string_val %}

        而不是

        {% url 'my_view' 'string_val' %}

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 在poll.id中应该是小写的p,而不是大写,大写用于类名
          【解决方案6】:

          还有,在

          投票/urls.py

          我有拼写错误

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

          vs 正确的代码

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

          花了一些时间寻找错误,所以寻找正确的拼写。哈哈

          【讨论】:

            【解决方案7】:

            在更正views.py中的过滤条件后,错误得到了解决。

            sn-p of my views.py

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

            来自我的models.py的sn-p

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

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

            我的 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 |
            +---------------------------------------+-----------+
            

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

            【讨论】:

              【解决方案8】:

              请注意您的主键数据类型。就我而言,我错误地使用了 int 而不是 str。

              如果 pk 是字符串,

               path('addesm/pending/<str:pk>', views.addesm, name='add ESM')
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-02
                • 2016-11-23
                • 1970-01-01
                • 2016-05-01
                • 2016-05-24
                • 1970-01-01
                相关资源
                最近更新 更多