我是初学者,正在尝试django。经过大量搜索之后,我仍然无法找到为什么出现NoReverseMatch错误。我的代码在下面,非常感谢您的帮助。我觉得这是一个愚蠢的错误。
我的Urls.py
urlpatterns=[
path('signup/', views.SignUpView, name='signup'),
path('checklist/',views.checklist,name='checklist'),
path('mybatches/',views.MyBatches.as_view(),name='mybatches'),
path('chklistforbatch/<int:pk>',views.CheckListForBatches.as_view(),name='chklistforbatch'),
]
我已经将这些URL包含在项目url.py中。
我的Views.py
class CheckListForBatches(LoginRequiredMixin, generic.ListView):
model = ChkListForBatch
form_class = ChkListForBatchForm
template_name = 'checklistforbatch.html'
def get_queryset(self):
batch_id = self.kwargs.get('pk')
print(self.kwargs.get('pk'))
selected_batch = CourseBatch.objects.filter(id=batch_id)
print(selected_batch[0].pk)
chkListItems = ChkListForBatch.objects.filter(batch=selected_batch[0].pk)
print(chkListItems[0].chkpoint_done)
return chkListItems #batch[0].checklistitems.all()
def get(self, request, *args, **kwargs):
print("inside get")
form = self.form_class()
obj_list = self.get_queryset()
return render(request, self.template_name, {'object_list':obj_list})
def post(self, request, *Args, **kwargs):
form = self.form_class(request.POST)
print("inside post")
print(form)
if form.is_valid():
return HttpResponseRedirect("/accounts/mybatches/")
return render(request,'checklistforbatch.html',{'form': form})
我的模板
{% block title %}My Batches{% endblock %}
{% block content %}
<h1>{{object_list.0.pk}}</h1>
<form action="{% url 'chklistforbatch' object_list.0.pk %}" method="POST">
{% csrf_token %}
{% for field in object_list %}
<li>
<label><input {% if field.chkpoint_done %} checked="checked" {% endif %} type="checkbox" name="{{ field }}" value="{{ field }}">{{field}}</label>
</li>
{% endfor %}
<button type="submit" value="POST">Update</button>
</form>
{% endblock %}
我得到的错误如下
django.urls.exceptions.NoReverseMatch:反转为“ chklistforbatch” 找不到带有参数'('',)'的参数。尝试了1种模式: ['accounts / chklistforbatch /(?P [0-9] +)$']