I have a FORMVIEW where a user selects a value from a dropdown, and if they select the value, and click submit, it performs an HTTPResponseRedirect to a URL and all is well.
If they forget to enter a value and click submit, they get an error message telling them to choose a value. I do a clean on the form to figure this out. This all works just fine.
The issue is if the user gets an error message, then adds a value and then displays the desired output. If they then click the back button on the browser window after viewing the output, the error message is still displayed.
I have been able to work around this by including a NeverCacheMixin to the form, but when the user clicks on the back button, they get an ugly Confirm Form Resubmission page.
The user is just looking up a value, so maybe there is a better way to approach this? There are no updates or deletes involved, it's strictly a lookup on one page that does an HttpResponseRedirect to another.
Here is my FORMVIEW...
class AuthorLookupView(LoginRequiredMixin,NeverCacheMixin,FormView):
form_class = AuthorLookup
template_name = 'author_lookup.html'
def form_valid(self, form):
authorbyname = form.cleaned_data['dropdown']
return HttpResponseRedirect(reverse('Books:author_detail',kwargs = { 'pk' : authorbyna
me.pk }))
Here is my FORM....
class AuthorLookup(forms.Form):
dropdown = forms.ModelChoiceField(queryset=User.objects.none(),required=False)
def __init__(self, *args, **kwargs):
super(AuthorLookup, self).__init__(*args, **kwargs)
self.fields['dropdown'].widget.attrs['class'] = 'name'
def clean(self):
cleaned_data = super(AuthorLookup, self).clean()
dropdown = cleaned_data.get('dropdown')
if dropdown:
pass
else:
self.add_error('dropdown','Author is required.')
pass
return cleaned_data
In a perfect world I'd like to delete the error message upon a successful clean so that if the user clicked on the browser back button they would see the form and not see the "leftover" error message. Is there a way to delete the error message from the browser memory upon successful submission, or is my current work around with the NeverCacheMixin the best approach to this problem?
I realize I could make my forms.ModelChoiceField required, but I am trying to customize the error messages throughout my app, so I've already ruled that out as a possibility.
Thanks in advance for any thoughts.