[Fixed]-Django: NoReverseMatch Error production_id: None

1👍

It looks like production_id can be None in your view, in which case you can’t use it when you call reverse. It would be better to use production.id instead. You have just saved the production in your view, so production.id will be set.

return HttpResponseRedirect(reverse('podfunnel:episodeimagefiles', kwargs={'production_id':production.id}))

Note that you can simplify this line by using the redirect shortcut. Add the import,

from django.shortcuts import redirect

then change the line to

return redirect('podfunnel:episodeimagefiles', production_id=production.id)

0👍

You can’t always redirect to episodeimagefiles if you didn’t provide appropriate initial value for production_id:

    # See if a production_id is passed on the kwargs, if so, retrieve and fill current data.
    # if not just provide empty form since will be new.
    production_id = kwargs.get('production_id', None)  <-- here you set production_id variable to None if no `production_id` in kwargs

Look at your exception:

Exception Value: Reverse for 'episodeimagefiles' with arguments '()' and keyword arguments '{'production_id': None}' not found. 1 pattern(s) tried: [u'podfunnel/episodeimagefiles/(?P<production_id>[0-9]+)/$']

It means you passed None value for production_id variable, but episodeimagefiles pattern required some int value to resolve url, so it raises NoReverseMatch exception.

Your form is valid in EpisodeInfoView.post because you set required=False for production_id attribute in your form:

class EpisodeInfoForm(forms.Form):

    production_id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)

I guess, if you debug your generated form before submit it, you can see something like <input type="hidden" name="production_id" value="None" />

Leave a comment