0

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.

Steve Smith
  • 1,019
  • 3
  • 16
  • 38

1 Answers1

0

So this one was tricky because at some point I thought autocomplete=off solved it but it didn't. Ultimately, this I found that using the code below actually fixed my issue. I can see the error message momentarily...but then it does disappear. Looks like this is the best that I can do to clear out the cache if the user hits the back button....

    $(window).bind("pageshow", function(event) {
        if (event.originalEvent.persisted) {
            // window.location.reload()
            window.location.reload(true);
        }
    });

And after a day of testing....I did in fact have to add autocomplete="off" back into my project for other use cases and now I am no longer seeing even a flash of the error message that I reported previously. Whether that's coincidental or not at all related is hard to tell. Either way adding both of these bits of code seems to have resolved my issue. autocomplete="off" in the HTML header and the code above seems to resolve my issue entirely.

HTML...

<form method="GET" autocomplete="off" action="{% url 'your_url_here' %}">
Steve Smith
  • 1,019
  • 3
  • 16
  • 38