我是 Django 新手,正在尝试让用户从选项下拉列表中进行选择,然后将他们的选择传递给我的下一个视图类,以便可以编辑记录。现在我的代码传递了疾病的名称,但没有传递数据库中的 PK。这似乎是一个简单的问题,但我不知道如何解决它。我收到以下错误:
Reverse for 'drui' with arguments '('',)' and keyword arguments '{}' not found.
代码如下:
视图.py
def drui_index(request):
diseaseForm = DiseaseForm(request.POST)
if diseaseForm.is_valid():
#the problem is probably in the below line. The code isn't right.
new_disease = diseaseForm.cleaned_data['disease']
url = reverse('drui', kwargs={'someApp_disease_id': new_disease.pk})
return HttpResponseRedirect(url)
else:
diseaseForm = DiseaseForm()
return render_to_response("drui_index.html", {'diseaseForm': diseaseForm}, context_instance=RequestContext(request))
def drui(request, someApp_disease_id):
disease = get_object_or_404(Disease, pk=someApp_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)
if indicatorInlineFormSet.is_valid():
new_disease.save()
indicatorInlineFormSet.save()
return HttpResponseRedirect(reverse(valdrui))
else:
diseaseForm = DiseaseForm(instance=disease)
indicatorInlineFormSet = IndicatorFormSet(instance=disease)
return render_to_response("drui.html", {'diseaseForm': diseaseForm, 'indicatorInlineFormSet': indicatorInlineFormSet, 'hide_breadcrumb': hide_breadcrumb},context_instance=RequestContext(request))
表格.py
class DiseaseForm(forms.ModelForm):
disease = forms.ModelChoiceField(queryset=Disease.objects.all())
class Meta:
model = Disease
网址.py
url(r'^drui_index/$', 'someApp.views.drui_index', name='drui_index'),
url(r'^drui/(?P<someApp_disease_id>\d+)/$', 'someApp.views.drui', name='drui')
drui.html 的 HTML
<form class="disease_form" action="{% url drui someApp_disease_id %}" method="post">{% csrf_token %}
{{ diseaseForm.as_table }}
{{ indicatorInlineFormSet.as_table }}
drui_index.html 的 HTML
<form class="disease_form" action="{% url drui_index %}" method="post">{% csrf_token %}
{{ diseaseForm.as_table }}
更新
通过在我的 kwargs 中添加一个 .pk 来解决它。但现在我得到了一个“drui”的反向,参数“(”,)”和关键字参数“{}”没有找到。