[Answer]-Django NoReverseMatch Reverse for DetailView

1👍

The view required passing the primary key as kwarg, using the name of the url.py entry (plot_detail), and using the namespace appended to it (‘geoapp:plot_detail’) to make things work.

def InventoryDataAdd(request, forestinventoryplot_id=1):
    if request.method == 'POST':
        form = InventoryDataForm(request.POST) 
        if form.is_valid(): 
            new_data = form.save()
            return HttpResponseRedirect(reverse('geoapp:plot_detail', kwargs={'pk':new_data.forestinventoryplot_id}))
        else: 
            initial_data = {'forestinventoryplot' : forestinventoryplot_id}
            form = InventoryDataForm(initial=initial_data)
    return render(request, 'geoapp/forestinventorydata_add.html', {'form': form})
👤akthor

Leave a comment