0

过去一天我一直遇到这个错误,似乎无法解决。

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中选择了一种疾病。但是,我不认为这是导致问题的原因。

4

2 回答 2

3

您的url标签应该是{% url "drui" disease_id=disease.id %},因为您需要传入关键字参数。

有关更多信息,请参阅文档

由于您从不保存新条目,我认为您只是使用表单来显示ModelChoiceField,在这种情况下,您不需要ModelForm

class DiseaseForm(forms.Form):
    disease = forms.ModelChoiceField(queryset=Disease.objects.all())

这样,您就避免了该commit=False部分。

你应该总是有一个else适合你的if form.is_valid()

from django.shortcuts import redirect

def drui(request, disease_id):
   disease = get_object_or_404(Disease, pk=disease_id)
   ctx = {}
   ctx['disease'] = disease  
   ctx['indicatorInlineFormSet'] = IndicatorFormSet()
   ctx['diseaseForm'] = DiseaseForm()

   if request.method == "POST":

      diseaseForm = DiseaseForm(request.POST)
      indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES)

      if diseaseForm.is_valid():
         return redirect('drui', disease_id=disease_id)
      else:
         # Form wasn't valid, return the same view to display the errors
         ctx['diseaseForm'] = diseaseForm
         ctx['indicatorInlineFormset'] = indicatorInlineFormset 
         return render(request, 'drui.html', ctx)

   else:

       return render(request, 'drui.html', ctx)
于 2013-08-04T23:38:00.537 回答
3

我认为您在 url 名称周围缺少引号:

{% url drui disease.id %}

应该是(如果您使用 django >= 1.5)或{% load url from future %}在模板中使用:

{% url "drui" disease.id %}
于 2013-08-04T23:32:39.523 回答