Django表示反向为''与参数'(7,)'和关键字参数'{}'未找到 [英] Django forms Reverse for '' with arguments '(7,)' and keyword arguments '{}' not found
问题描述
我过去一天遇到这个错误,似乎无法解决。
I have been running into this error for the past day and can't seem to solve it.
Reverse for '' with arguments '(7,)' and keyword arguments '{}' not found.
我的目标是让用户从我的drui_index页面选择一种疾病。然后将用户带到drui页面添加或编辑指标。在我的模型中,每个疾病都有多个指标。当我到达drui页面,我得到上面的错误,我不知道为什么。错误中的7是疾病表中的PK。
My goal is to have a user select a disease from my drui_index page. Then the user is taken to the drui page to add or edit the indicators. In my models, there are multiple indicators for each disease. When I get to the drui page, I get the error above and am not sure why. The '7' in the error is the PK from the disease table.
Views.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 }}
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'),
forms.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中选择了一种疾病。但是,我不认为这是导致这个问题。
In my views.py, I don't think I need to call the diseaseForm because I have already chosen a disease in drui_index. However, I don't think that's causing the problem.
推荐答案
我觉得你在url名字中缺少引号:
I think you are missing quotes around url name:
{% url drui disease.id %}
应该是(如果你使用django> = 1.5)或者使用 {%从未来%}加载url
在模板中:
Should be (if you are using django >= 1.5) or using {% load url from future %}
in template:
{% url "drui" disease.id %}
这篇关于Django表示反向为''与参数'(7,)'和关键字参数'{}'未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!