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.
So after chasing my tail on this one...I'll document for any future mes looking for an answer to this. In order to prevent the error message from showing up with the user hits the back button...
In your HTML....add....
autocomplete=off
in the form header so that it looks something like...
Adding autocomplete=off prevents the error message from still being displayed if the user moves on from the form with the error message and then hits the back button to go back to the prior form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.