过去一天我一直遇到这个错误,似乎无法解决。
Reverse for '' with arguments '(7,)' and keyword arguments '{}' not found.
我的目标是让用户从我的 drui_index 页面中选择一种疾病。然后将用户带到 drui 页面以添加或编辑指标。在我的模型中,每种疾病都有多个指标。当我进入德鲁伊页面时,我得到了上面的错误,不知道为什么。错误中的“7”是疾病表中的 PK。
视图.py
def drui_index(request):
disease_list = Disease.objects.all()
context = {'disease_list':disease_list}
return render(request, 'drui_index.html', context)
def drui(request, disease_id):
disease = get_object_or_404(Disease, pk=disease_id)
if request.method == "POST":
diseaseForm = DiseaseForm(request.POST, instance=disease)
indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES, instance=disease)
if diseaseForm.is_valid():
new_disease = diseaseForm.save(commit=False)
indicatorInlineFormSet.save()
return HttpResponseRedirect(reverse('drui', kwargs={'disease_id':disease_id}))
else:
diseaseForm = DiseaseForm(instance=disease)
indicatorInlineFormSet = IndicatorFormSet(instance=disease)
return render(request, 'drui.html', {'disease':disease, 'diseaseForm':diseaseForm, 'indicatorInlineFormSet': indicatorInlineFormSet})
HTML drui.html
<form class="disease_form" action="{% url drui disease.id %}" method="post">{% csrf_token %}
{{ disease }}
{{ diseaseForm.as_table }}
{{ indicatorInlineFormSet.as_table }}
网址.py
url(r'^drui_index/$', 'Physician_UI.views.drui_index', name='drui_index'),
url(r'^drui_index/(?P<disease_id>\d+)/$', 'Physician_UI.views.drui', name='drui'),
表格.py
class DiseaseForm(forms.ModelForm):
disease = forms.ModelChoiceField(queryset=Disease.objects.all())
class Meta:
model = Disease
IndicatorFormSet = inlineformset_factory(Disease,
Indicator,
can_delete=False,
extra=MAX_INDICATORS)
在我的views.py中,我认为我不需要调用diseaseForm,因为我已经在drui_index中选择了一种疾病。但是,我不认为这是导致问题的原因。