我是 Django 新手,所以这可能是一个简单的问题。我正在尝试将 url 传递给我的 views.py 中的另一个类。这个 url 只是我的数据库表中的一个 PK id。我不断收到一条错误消息:
Reverse for ''drui'' with arguments '('',)' and keyword arguments '{}' not found
我的代码如下:
网址.py
url(r'^drui/(?P<disease_id>\d+)/$', 'myapp.views.drui', name='drui')
HTML
drui_index.html
<form class="disease_form" action="{% url drui_index %}" method="post">{% csrf_token %}
<div class="form-block">
{{ diseaseForm.as_table }}
</div>
</form>
德鲁伊.html
<form class="disease_form" action="{% url 'drui' disease_id.id %}" method="post">{% csrf_token %}
<div class="form-block">
{{ diseaseForm.as_table }}
{{ indicatorInlineFormSet.as_table }}
</div>
</form>
视图.py
def drui_index(request):
diseaseForm = DiseaseForm(request.POST)
if diseaseForm.is_valid():
new_disease = diseaseForm.save()
url = reverse('drui', kwargs={'disease_id': new_disease.id})
return HttpResponseRedirect(url)
else:
diseaseForm = DiseaseForm()
return render_to_response("drui_index.html", {'diseaseForm': diseaseForm}, context_instance=RequestContext(request))
#I am passing disease_id to the class below
def drui(request, disease_id):
disease = get_object_or_404(Disease, pk=disease_id)
....