Django反向为''与参数'()'和关键字参数'{'}'未找到错误 [英] Django Reverse for '' with arguments '()' and keyword arguments '{'}' not found Error

查看:154
本文介绍了Django反向为''与参数'()'和关键字参数'{'}'未找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 使用参数'('',''和关键字参数'{} ' 未找到。 

代码如下:



views.py

  def drui_index(request):
diseaseForm = DiseaseForm(request.POST)

如果diseaseForm.is_valid():
#问题可能在下面的一行。代码不正确。
new_disease = diseaseForm.cleaned_data ['disease']
url = reverse('drui',kwargs = {'someApp_disease_id':new_disease.pk})
return HttpResponseRedirect(url)

else:
diseaseForm = DiseaseForm()

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



def drui(request,someApp_disease_id):

疾病= get_object_or_404(疾病,pk = someApp_disease_id

如果请求。方法==POST:

diseaseForm = DiseaseForm(request.POST,instance = disease)
indicatorInlineFormSet = IndicatorFormSet(request.POST,request.FILES,instance = disease)
如果diseaseForm.is_valid():
new_disease = diseaseForm.save(commit = False)

如果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 = b $ b

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')

H tML for drui.html

 < form class =disease_formaction ={%url drui someApp_disease_id% }method =post> {%csrf_token%} 
{{diseaseForm.as_table}}
{{indicatorInlineFormSet.as_table}}

drui_index.html的HTML

 < form class =disease_formaction ={%url drui_index%}method =post> {%csrf_token%} 
{{diseaseForm.as_table}}

更新



pk在我的kwargs。但是现在我得到一个反向的'drui'与参数'('',)'和关键字参数'{}'没有找到。

解决方案>

drui.html 中,您是指在上下文中不存在的 someApp_disease_id ,这给出你的第一个错误是原来的问题。为了解决这个问题,只需使其可用。



我不知道应该将哪个ID存储在此变量中,但您应该在 drui 查看

 返回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 视图中,您修复了另一个问题(使用参数'()'''反向'',关键字参数'{'someApp_disease_id':<疾病:someDisease>}'未找到。),这允许您去第二个错误,这是我上面已经修复的错误。所以基本上你应该将以上内容添加到 drui 中,并将 .pk drui_index 。这应该可以解决你的问题。


I am new to Django and am trying to have a user select from a drop-down of choices, then have their choice pass to my next view class so that the record can be edited. Right now my code passes the name of the disease but not the PK from the database. It seems like a simple problem but I'm not sure how to solve it. I get the following error:

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

Code is below:

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')

HTML for drui.html

<form class="disease_form" action="{% url drui someApp_disease_id %}" method="post">{% csrf_token %}
    {{ diseaseForm.as_table }}
    {{ indicatorInlineFormSet.as_table }}

HTML for drui_index.html

<form class="disease_form" action="{% url drui_index %}" method="post">{% csrf_token %}
 {{ diseaseForm.as_table }}

UPDATE

Solved it by adding a .pk in my kwargs. But now I get a Reverse for 'drui' with arguments '('',)' and keyword arguments '{}' not found.

解决方案

In drui.html you are referring to someApp_disease_id which doesn't exist in the context, this gives you your first error, the one in the original question. To fix this, simply make it available.

I don't know which id is supposed to be stored in this variable but you should have something like this in the drui view

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

When you added .pk to the drui_index view, you fixed the other issue ("Reverse for 'drui' with arguments '()' and keyword arguments '{'someApp_disease_id': <Disease: someDisease>}' not found.") which allowed you to go to the second error which is the one I have fixed above. So basically you should add the above to drui and leave the .pk in drui_index. That should cure your problems.

这篇关于Django反向为''与参数'()'和关键字参数'{'}'未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆