Django反向''带参数'('',)'和关键字参数'{}'找不到错误

时间:2013-08-05 01:59:05

标签: django

我收到此错误

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

我正在渲染我的选项页面。选项页面是3部分工作流程中的最后一页。前2页工作正常。我不确定为什么没有渲染。我的代码如下:

views.py

def drui_index(request):
  disease_list = Disease.objects.all()
  context = {'disease_list':disease_list, 'hide_breadcrumb': hide_breadcrumb}
  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":
      indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES, instance=disease)

      if indicatorInlineFormSet.is_valid():
         indicatorInlineFormSet.save()
         return HttpResponseRedirect(reverse('option', kwargs={'disease_id':disease_id}))

  else:
      indicatorInlineFormSet = IndicatorFormSet(instance=disease)

  return render(request, 'drui.html', {'disease':disease, 'indicatorInlineFormSet': indicatorInlineFormSet})


def option(request, disease_id):

     disease = get_object_or_404(Disease, pk=disease_id)

     if request.method == "POST":
         optionInlineFormSet = OptionFormSet(request.POST, request.FILES, instance=disease, prefix='option')
         outcomeInlineFormSet = OutcomeFormSet(request.POST, request.FILES, instance=disease, prefix='outcome')


        if optionInlineFormSet.is_valid() and outcomeInlineFormSet.is_valid:
           optionInlineFormSet.save()
           outcomeInlineFormSet.save()
           return HttpResponseRedirect(reverse('splash', kwargs={'disease_id':disease_id}))

    else:
        optionInlineFormSet = OptionFormSet(instance=disease,prefix='option')  
        outcomeInlineFormSet = OutcomeFormSet(instance=disease,prefix='outcome')

   return render(request, 'option.html', {'optionInlineFormSet': optionInlineFormSet, 'outcomeInlineFormSet': outcomeInlineFormSet})  

urls.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'),
url(r'^drui_index/(?P<disease_id>\d+)/option/$', 'Physician_UI.views.option', name='option'),

HTML drui_index.html

{% load url from future %}
<form class="disease_form" action="{% url "drui_index" %}" method="post">{% csrf_token %}

HTML drui.html

{% load url from future %}
<form class="disease_form" action="{% url "drui" disease.id %}" method="post">{% csrf_token %}

HTML option.html

 {% load url from future %}
 <form class="option_form" action="{% url "option" disease.id %}" method="post">{% csrf_token %}

正如我之前提到的,前两页(drui_index和drui)工作正常但是option.html没有。我包含了大部分代码,以防万一我以错误的方式传递信息。

我正在使用django 1.4

1 个答案:

答案 0 :(得分:1)

您应该在disease视图的上下文中传递option个对象。您没有在上下文中传递disease对象,而在模板中使用disease.id因此错误。

return render(request, 'option.html',
    {'optionInlineFormSet': optionInlineFormSet,
    'outcomeInlineFormSet': outcomeInlineFormSet,
    'disease': disease})