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:{%静态加载 %} 2:3:4:
5:6 :{%标题%} 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” 在render_content中 83. content = template.render(context,self._request)
文件 “ C:\ Users \ Sid \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ template \ backends \ django.py” 在渲染 61.返回self.template.render(context)
文件 “ C:\ Users \ Sid \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ template \ base.py” 在渲染 175. return 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。位= node.render_annotated(上下文)
文件 “ 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. return created_parent._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。位= node.render_annotated(上下文)
文件 “ 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. result = block.nodelist.render(context)
文件 “ C:\ Users \ Sid \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ django \ template \ base.py” 在渲染 943。位= node.render_annotated(上下文)
文件 “ 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.raiseNoReverseMatch(msg)
异常类型:/ suppliers / quiz / 1 / results /异常中的NoReverseMatch 值:找不到带有参数'('',)'的'quiz_change'的反转。 1个 尝试过的模式:['供应商/测验/确认/(?P [0-9] +)/ $']
答案 0 :(得分:2)
在该视图中,没有任何名为activetruck
的东西。您的上下文对象名称为quiz
,您已经在同一行上使用它了;您需要保持一致。
<li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change' quiz.pk %}">{{ quiz.name }}</a></li>
答案 1 :(得分:0)
<li class="breadcrumb-item"><a href="{% url 'suppliers:quiz_change' quiz.id %}">{{ quiz.name }}</a></li>
只需将quiz.pk
更改为quiz.id
。