[Fixed]-Django NoReverseMatch at /

0👍

I’ve found the answer:

views.py

def day_data_multiadd(request, no_of_lines=0):
no_of_lines = int(no_of_lines)
CostFormSet = modelformset_factory(Cost, form=DataAddForm, extra=no_of_lines)

if request.method == 'POST' and 'form' in request.POST:
    formset = CostFormSet(request.POST, request.FILES)
    if formset.is_valid():
        formset.save()
else:
    formset = CostFormSet(queryset=Cost.objects.none())

if request.method == 'POST' and 'no_line' in request.POST:
    generate_form = MultiaddGenerateForm(request.POST)
    if generate_form.is_valid():
        cd = generate_form.cleaned_data
        return HttpResponseRedirect(reverse('core_sm:day_data_multiadd', args=(cd['formy'], )))
else:
    generate_form = MultiaddGenerateForm()

return render(request, 'core_sm/costs/multi_add.html', {'formset': formset,
                                                        'no_of_lines': no_of_lines,
                                                        'generate_form': generate_form})

url.py

url(r'^day_data_multiadd/$', views.day_data_multiadd, name='day_data_multiadd'),
url(r'^day_data_multiadd/(?P<no_of_lines>\d+)/$', views.day_data_multiadd, name='day_data_multiadd'),

1👍

You’ve called this somewhere:

{% url day_data_multiadd %}

where you should provide the number of lines:

{% url day_data_multiadd number_of_lines %}

If you want to add a default value, so the first example would work, you must add an other url pattern and provide a default value in the view:

...
url(r'^day_data_multiadd/$', views.day_data_multiadd, name='day_data_multiadd'),
url(r'^day_data_multiadd/(?P<no_of_lines>\d+)/$', views.day_data_multiadd, name='day_data_multiadd'),
...

...
def day_data_multiadd(request, no_of_lines=0):
    ...

Leave a comment