找不到带有参数“('',)”的“详细信息”。尝试了2种模式:

时间:2019-01-16 13:32:23

标签: django

我似乎无法在代码中发现错误,并且已经尝试了所有方法。这可能是一件简单的事情,使我无法逃脱。请帮忙!任何输入表示赞赏。此处是新的Django学习者。

Traceback (most recent call last):
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\doctrinality\doctrinality\Testimony\views.py", line 175, in TestimonyOrderView
    return render(request, 'testimony_post.html', {'queryset_list':queryset_list})
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 188, in render
    return template.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 173, in render
    return self._render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 442, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\base.py", line 90, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'details' with arguments '('',)' not found. 2 pattern(s) tried: ['(?P<id>[0-9]+)/details(?P<format>\\.[a-z0-9]+/?)$', '(?P<id>[0-9]+)/details/$']
[16/Jan/2019 21:58:50] "GET /testimonypost/ HTTP/1.1" 500 187935

模板

{%for objects in queryset_list%}
<div class="w3-card-4">
  <header class="w3-container w3-blue">
    <h1><a href="{% url 'details' testimony.id %}"</a>{{testimony.Title}}</h1>
  </header>

  <div class="w3-container">
    <p>{{objects.body}}</p>
  </div>

  <footer class="w3-container w3-blue">

  </footer>

</div>
<form action="{% url 'testimonydelete'%}" method="POST" style="display">
<button class="glyphicon glyphicon-trash"></button>
</form>
{%endfor%}

models.py

class Testimony(models.Model):
    ...

    def __str__(self):
        return int(self.id)

urls.py

urlpatterns = [
    ...
    path('<int:id>/details/', views.detail, name='details'),
]

views.py

def details(request, id=None):
    print('1')
    testimony=get_object_or_404(Testimony, id=id)
    print('2')
    return render(request, 'details.html', {'testimony': testimony})

1 个答案:

答案 0 :(得分:0)

出于某种原因,for循环中的变量名称实际上是objects。因此,就像您使用objects.body来显示正文一样,您需要在URL中使用objects.id

<h1><a href="{% url 'details' objects.id %}"</a>{{ objects.Title }}</h1>