【问题标题】:Reverse for 'quiz_change' with arguments '('',)' not found. 1 pattern(s) tried: ['suppliers/quiz/(?P<pk>[0-9]+)/$']未找到参数 '('',)' 的 'quiz_change' 反向。尝试了 1 种模式:['suppliers/quiz/(?P<pk>[0-9]+)/$']
【发布时间】:2019-09-07 15:35:37
【问题描述】:

urls.py

from django.urls import include, path

from .views import classroom, suppliers, teachers
urlpatterns = [
    path('', classroom.home, name='home'),

    path('suppliers/', include(([
        path('', suppliers.QuizListView.as_view(), name='quiz_list'),
        path('interests/', suppliers.SupplierTruckView.as_view(), name='supplier_trucks'),
        path('taken/', suppliers.TakenQuizListView.as_view(), name='taken_quiz_list'),
        path('quiz/<int:pk>/', suppliers.take_quiz, name='take_quiz'),
        path('quiz/edit/<int:pk>/', suppliers.edit_quiz, name='edit_quiz'),

        # teachers modules
        path('quiz/add/', suppliers.QuizCreateView.as_view(), name='quiz_add'),
        path('quiz/confirm/<int:pk>/', suppliers.QuizUpdateView.as_view(), name='quiz_change'),

        path('quiz/<int:pk>/delete/', suppliers.QuizDeleteView.as_view(),name='quiz_delete'),

        path('quiz/<int:pk>/question/add/', suppliers.question_add, name='question_add'),
        path('quiz/<int:quiz_pk>/question/<int:question_pk>/', suppliers.question_change, name='question_change'),

        path('myposts', suppliers.QuizListView1.as_view(), name='quiz_change_list'),
        path('quiz/<int:pk>/results/', suppliers.QuizResultsView.as_view(), name='truck_results'),


    ], 'classroom'), namespace='suppliers')),

views.py

class QuizUpdateView(UpdateView):
    model = Activetruck
    fields = ('name', 'subject', 'origin', 'destination','total_trucks','scheduled_date','offered_price',)
    context_object_name = 'quiz'
    template_name = 'classroom/suppliers/quiz_change_form.html'

    def get_context_data(self, **kwargs):
        kwargs['questions'] = self.get_object().questions1.annotate(answers_count=Count('answers1'))
        return super().get_context_data(**kwargs)

    def get_queryset(self):
        return self.request.user.activetruck.all()

    def get_success_url(self):
        return reverse('suppliers:quiz_change', kwargs={'pk': self.object.pk})

当我的 django 应用尝试访问下面提到的 URL 时:

 path('quiz/confirm/<int:pk>/', suppliers.QuizUpdateView.as_view(), name='quiz_change'),

它显示 NoReverseMatch 错误。我无法弄清楚这是从哪里来的。我也应该添加回溯吗?

这是 QuizUpdateView 中的模板:

    <nav aria-label="breadcrumb">
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change_list' %}">My Quizzes</a></li>
      <li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change' activetruck.pk %}">{{ quiz.name }}</a></li>
      <li class="breadcrumb-item active" aria-current="page">Results</li>
    </ol>
  </nav>
  <h2 class="mb-3">{{ quiz.name }} Results</h2>

  <div class="card">
    <div class="card-header">
      <strong>Posted Requests For Quotes</strong>
      <span class="badge badge-pill badge-primary float-right">Least Bid: {{ quiz_score.least_bid|default_if_none:0.0 }}</span>
    </div>
    <table class="table mb-0">
      <thead>
        <tr>
          <th>supplier</th>
          <th>Date</th>
          <th>Bid Amount</th>
        </tr>
      </thead>
      <tbody>
        {% for taken_quiz in taken_quizzes %}
          <tr>
            <td>{{ taken_quiz.supplier.user.username }}</td>
            <td>{{ taken_quiz.date|naturaltime }}</td>
            <td>{{ taken_quiz.least_bid }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
    <div class="card-footer text-muted">
      Total respondents: <strong>{{ total_taken_quizzes }}</strong>
    </div>
  </div>

我想错误在模板文件中,因为我已经彻底检查了代码

追溯:

模板错误:在模板中 C:\Users\Sid\Downloads\aggregator-master\django_school\templates\base.html, 第 0 行错误 使用参数 '('',)' 反转'quiz_change' 未找到。尝试了 1 种模式: ['suppliers/quiz/confirm/(?P[0-9]+)/$'] 1 : {% load static %} 2 : 3 : 4 :
5 : 6 : {% block title %}Yantraksh Freight{% endblock %} 7 : 8 : 9:10:

追溯:

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py” 在内部 35. response = get_response(request)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py” 在 _get_response 158. response = self.process_exception_by_middleware(e, request)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py” 在 _get_response 156. response = response.render()

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py” 在渲染中 106. self.content = self.rendered_content

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py” 在渲染内容中 83. content = template.render(context, self._request)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py” 在渲染中 61. return self.template.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在渲染中 175. 返回 self._render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在_render 167. return self.nodelist.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在渲染中 943. bit = node.render_annotated(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在 render_annotated 910. return self.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py” 在渲染中 155. 返回已编译的_parent._render(上下文)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在_render 167. return self.nodelist.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在渲染中 943. bit = node.render_annotated(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在 render_annotated 910. return self.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py” 在渲染中 67. 结果 = block.nodelist.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在渲染中 943. bit = node.render_annotated(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py” 在 render_annotated 910. return self.render(context)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py” 在渲染中 447. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\base.py” 相反 88. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

文件 “C:\Users\Sid\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py” 在 _reverse_with_prefix 632. raise NoReverseMatch(msg)

异常类型:NoReverseMatch at /suppliers/quiz/1/results/ 异常 值:未找到参数 '('',)' 的 'quiz_change' 反向。 1 尝试的模式:['suppliers/quiz/confirm/(?P[0-9]+)/$']

【问题讨论】:

  • 请显示出现错误的视图的模板。
  • 更新问题,请立即查看
  • 该模板没有引用 quiz_change URL。请显示完整的回溯,以便我们查看错误的实际来源。
  • 能否请您搜索您的模板并找到实际正在执行的模板{% url 'quiz_change' ... %}。到目前为止,您还没有发布它,所以我们无法为您提供帮助。
  • @DanielRoseman 这样好吗?请立即查看

标签: python django django-models django-templates


【解决方案1】:
<li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change' quiz.id %}">{{ quiz.name }}</a></li>

只需将quiz.pk 更改为quiz.id

【讨论】:

    【解决方案2】:

    您在该视图中没有任何名为 activetruck 的内容。您的上下文对象名称是quiz,您已经在同一行中使用了它;你需要保持一致。

    <li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change' quiz.pk %}">{{ quiz.name }}</a></li>
    

    【讨论】:

    • 您能详细说明一下吗?
    猜你喜欢
    • 2020-05-13
    • 2021-08-10
    • 2019-02-07
    • 2019-11-18
    • 2020-10-21
    • 2022-01-13
    • 2020-11-06
    • 2021-06-13
    • 2020-02-06
    相关资源
    最近更新 更多