0

我是 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”的反向,参数“(”,)”和关键字参数“{}”没有找到。

4

3 回答 3

0

i suppose that 'drui' is another method in the views.py which contain another template you want to redirect to that when diseaseForm.is_valid()

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))
于 2013-08-04T18:04:27.493 回答
0

正如我之前提到的,至少看起来它cleaned_data只是返回 a CharField,而不是 a model,因此你正在通过一个 empty .pk

于 2013-08-04T19:41:38.457 回答
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))

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

于 2013-08-05T13:44:32.713 回答