Todd B
Todd B

Reputation: 105

Django Form Not Validating my Date fields

I have a Search form that includes the query box, some checkbox items, and a start and end date. I am getting my information when I enter a query, so I know it is performing the search_results action, but the start and end date is not getting validated at all. I have yet to add the code to handle the start and end date in my results, but the issue is there is no client side validation happening on the form.

My form looks like this:

class SearchForm(forms.Form):

    q = forms.CharField(max_length=64,required=False)
    service_choices = forms.MultipleChoiceField(
        required=True,
        widget=forms.CheckboxSelectMultiple,
        choices=CustomUser.SERVICE_CHOICES,
        )
    start_date = forms.DateField(required=False)
    end_date = forms.DateField(required=False)

I tried using Class Based View, but did not get any fields at all, so I tried using a function based view to get my search criteria.

The view looks like:

def get_search(request):
    form = SearchForm()
    return render(request, 'get_search.html', {'form':form})

My template is:

<!-- templates/get_search.html -->
{% extends "base.html" %}
{% block title %}Search{% endblock title %}
{% block content %}
{% if user.is_authenticated %}
  <br><br>
  <TABLE BORDER="0" WIDTH="100%">
        <TR>
        <TD ALIGN="Left"><B>Track X - search</B></TD>
        <TD ALIGN="Center"><B>User:&nbsp;{{ user }}</B></TD>
        <TD ALIGN="Right"><B>Service:&nbsp;{{ user.service }}</B></TD>
        </TR>
  </TABLE>


  <form action="{% url 'search_results' %}" method="get"">
  {% csrf_token %}
    <TABLE BGCOLOR="#66CCCC" Border="2" WIDTH="100%">
    <TR>
      <TD ALIGN="Left" WIDTH="100">&nbsp;&nbsp;Services:</TD>
        <TD ALIGN="Right">Search expression:</TD>
        <TD ALIGN="Left">
            {{ form.q }}
            <button type="submit" name="submit">Search TrackX</button>
        </TD>
    </TR>
    <TR>
      <TD WIDTH="100"> {{ form.service_choices }} </TD>
      <TD COLSPAN="2" ALIGN="Center"> Start Date:&nbsp;{{ form.start_date }}&nbsp;&nbsp;
      End Date:&nbsp;{{ form.end_date }}</TD>
    </TR>
    </TABLE>
  </form>
{% else %}
    <p>You are not logged in</p>
    <a href="{% url 'login' %}">Log In</a> |
    <a href="{% url 'signup' %}">Sign Up</a>
{% endif %}
{% endblock content %}

The Search_results view just uses the query to build some Q Filter queries, and it seems to be working correctly. It originally used a GET Method, and was passing the parameters into the search results view just fine. It just doesn't validate the dates (or anything else I assume) before passing the parameters into the view. Can anyone tell me what am I missing here?

Upvotes: 0

Views: 123

Answers (1)

mendespedro
mendespedro

Reputation: 516

As I see on the view that your posted, you are not actually checking if the form is valid, you can do that by using:

if request.method == 'POST':
   form = SearchForm(request.POST)
   form.is_valid()

This method will validate all your fields, and return the boolean value representing if its valid or not.

Upvotes: 2

Related Questions