Django Reverse for''参数'()'和关键字参数' {'}'找不到错误

时间:2013-08-04 17:09:26

标签: django

我是Django的新手,我正在尝试让用户从下拉选项中进行选择,然后将他们的选择传递给我的下一个视图类,以便可以编辑记录。现在我的代码传递疾病的名称,但不传递数据库中的PK。这似乎是一个简单的问题,但我不知道如何解决它。我收到以下错误:

 Reverse for 'drui' with arguments '('',)' and keyword arguments '{}' not found.

代码如下:

views.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))   

forms.py

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

class Meta:
   model = Disease

urls.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'的反转。

3 个答案:

答案 0 :(得分:0)

我认为'drui'是views.py中的另一种方法,它包含你想要重定向的另一个模板,当疾病发生时,它会发生疾病。(

views.py

from django.core import urlresolvers
def drui_index(request):
diseaseForm = DiseaseForm(request.POST)

if diseaseForm.is_valid():
   new_disease = diseaseForm.cleaned_data['disease']
   url = urlresolvers.reverse('drui', kwargs={'disease_id': new_disease})
   return HttpResponseRedirect(url)

else:
   diseaseForm = DiseaseForm()

return render_to_response("drui_index.html", {'diseaseForm': diseaseForm}, context_instance=RequestContext(request))

答案 1 :(得分:0)

正如我之前提到的,至少看来cleaned_data只返回CharField,而不是model,因此你正在通过一个空的{{} 1}}。

答案 2 :(得分:0)

drui.html中,您指的是上下文中不存在的someApp_disease_id,这会给您第一个错误,即原始问题中的错误。要解决此问题,只需将其设为可用。

我不知道哪个id应该存储在这个变量中,但你应该在drui视图中有这样的东西

return render_to_response("drui.html", 
              {'diseaseForm': diseaseForm,
              'indicatorInlineFormSet': indicatorInlineFormSet,
              'hide_breadcrumb': hide_breadcrumb,
              'someApp_disease_id': new_disease.id}, //<<-add this
              context_instance=RequestContext(request))

当您将.pk添加到drui_index视图时,您修复了另一个问题("Reverse for 'drui' with arguments '()' and keyword arguments '{'someApp_disease_id': <Disease: someDisease>}' not found."),该问题允许您转到我上面修复的第二个错误。所以基本上你应该将上面的内容添加到drui并将.pk保留在drui_index中。这应该可以解决你的问题。