Steve Smith
Steve Smith

Reputation: 1089

Django Formview Won't Validate

Django newbie here. I recently implemented a simple search with Django. I user specifies data in search bar, search is executed. I was able to determine today how to get the search to be null if user clicks on empty search bar. What I'm really trying to accomplish is to implement error logic so that the user has to specify data in the search bar. This has been very straight forward with all of the other views, but the search is a bit trickier, for me anyway. I have done several SO searches today, and determined that my HTML get method is ok for a search, but it may be causing some problems with the FORMVIEW that I am using. I have also tried to override POST and GET for the FORMVIEW, but can't seem to get it to work. I want to stick with CLASS BASED VIEWS, so I am using FORMVIEW. Here is my code....

My HTML

<form method="GET" autocomplete=off action="{% url 'Book:book_request_search_results' %}" >

<div>
  <h1 class="title">Book Request Search</h1>
</div>

 {{ form.non_field.errors }}
  <div class="section">
  <input type="search" class="name2" name="q">
  </div>

  <div class="section">
  <input type="submit" id="" class="submit6" value="Search">
  </div>

My VIEWS.PY

class BookRequestSearchView(LoginRequiredMixin,FormView):
    form_class = BookRequestSearch
    template_name = 'store/Book_request_search.html'
    success_url = reverse_lazy('store:book_request_search_results')

 def form_valid(self, form):
    self.kwargs['q'] = form.cleaned_data['q']
    if self.request.GET.get('q'):
         pass
    else:
         raise forms.ValidationError("Enter data")
         return super(BookRequestSearchView, self).form_valid(form)

My FORMS.PY

class BookRequestSearch(forms.Form):

q = forms.CharField(required=True)

def __init__(self, *args, **kwargs):
    super(BookRequestSearch, self).__init__(*args, **kwargs)

I also tried to add a clean method to my form but that doesn't seem to work either...

def clean(self):
    search = self.initial['q']
    if search:
        pass
    else:
        raise forms.ValidationError("Enter data")
    return self.cleaned_data

I've been scanning most of the afternoon and can't seem to find a way to trigger validation on the FORMVIEW to trigger an error on the search bar. Any help would be appreciated. I've seen many different FORMVIEW articles, but none of them have helped me understand what I'm doing wrong and why this isn't working. The way the code is now, it works, but it doesn't prevent user from clicking on search and then triggering essentially an empty query. I'm trying to prevent that by forcing the user to put something in the box if they try to search with no criteria. Thanks in advance for any direction.

Upvotes: 0

Views: 184

Answers (1)

schrodingerscatcuriosity
schrodingerscatcuriosity

Reputation: 1860

I was wrong on saying that it's only possible with javascript.

It's so simple as adding the required attribute to your input form, so the browser catches the empty error.

<form>
    <input type="search" required><!-- added the required attribute-->
</form>

Upvotes: 1

Related Questions