Reputation: 1635
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.
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)
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.
Upvotes: 0
Views: 195
Reputation: 39679
I think you are missing quotes around url name:
{% url drui disease.id %}
Should be (if you are using django >= 1.5) or using {% load url from future %}
in template:
{% url "drui" disease.id %}
Upvotes: 3
Reputation: 174642
Your url
tag should be {% url "drui" disease_id=disease.id %}
, because you need to pass in the keyword argument.
See the documentation for more information.
As you never save the new entry, I think you are just using the form to display the ModelChoiceField
, in that case you don't need a ModelForm
:
class DiseaseForm(forms.Form):
disease = forms.ModelChoiceField(queryset=Disease.objects.all())
That way, you avoid the commit=False
part.
You should always have an else
for your if form.is_valid()
:
from django.shortcuts import redirect
def drui(request, disease_id):
disease = get_object_or_404(Disease, pk=disease_id)
ctx = {}
ctx['disease'] = disease
ctx['indicatorInlineFormSet'] = IndicatorFormSet()
ctx['diseaseForm'] = DiseaseForm()
if request.method == "POST":
diseaseForm = DiseaseForm(request.POST)
indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES)
if diseaseForm.is_valid():
return redirect('drui', disease_id=disease_id)
else:
# Form wasn't valid, return the same view to display the errors
ctx['diseaseForm'] = diseaseForm
ctx['indicatorInlineFormset'] = indicatorInlineFormset
return render(request, 'drui.html', ctx)
else:
return render(request, 'drui.html', ctx)
Upvotes: 3