1

I want to use Datepicker(from Jquery) on django in this way:

Through the "views.py" i'm sending a list of dates from mysql DB. I want to display a message when a date that a user had chosen is equal to one of the dates i have in the DB.

How can i make this happen? (with Django, Jquery and AJAX)

My code so far:

{% block head %}

<link href="{{STATIC_URL}}css/jquery-ui.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript" src="{{STATIC_URL}}js/jquery1.min.js"></script>
<script src="{{STATIC_URL}}js/jquery-ui.min.js"></script>


<script>
$(function() {
    $( "#datepicker" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
    });
    date = $( "#datepicker" ).datepicker('getDate');
});
</script>

{% endblock %}

{% block body_block %}

<br/><br/><br/><br/><br/><br/><br/>
<form id="form" name="form" action="/home/" method="get">
    <label>Choose Date</label>
    <br/>
    <input style="color:black" id="datepicker"/>
    <br/><br/>
</form>

{% for date in dates %}
 ...............

Thanks.

user3731023
  • 65
  • 1
  • 7

1 Answers1

2

Here's a working example: http://jsfiddle.net/njsmu356/

Add all the dates to a div and hide it.

<div id="dateDiv" style="display:none;">
    <ul id="dates">
        {% for date in dates %} 
            <li>date</li> 
        {% endfor %}
    </ul>
</div>

Then use jQuery to find a match of the selected date with the dates in <li>.

    $('#datepicker').change(function() {
        // Get the value of date field
        var dateValue = $('#datepicker').val();

        // Make and empty list
        var dateList = [];

        // Append all dates from <li> to dateList
        $("#dates li").each(function() {
            dateList.push($(this).text()); 
        });

        // Perform the match    
        if ($.inArray(dateValue, dateList  ) > -1) {
            alert("Date matched!!!");
        } else {
            alert("Sorry. Try again.");
        }
    });

Note:

Be sure that the format of the selected date is same as that of the dates you are sending from your view. You may also need to validate the input value of the date. Some jQuery form validation plugins will help.

Update

Since you want to do this with AJAX, there are already so many answers accomplishing similar functionality.

  1. Django: ajax response for valid/available username/email during registration
  2. AJAX username validation in Django
  3. And a lot here
Community
  • 1
  • 1
xyres
  • 20,487
  • 3
  • 56
  • 85
  • Hi @xyres. I'm sure there is a more elegant way. It's not very efficient and secure in my opinion. My thought was running all over the dates until a date from my db is equal to the date the user picked from the datepicker. If you can show my a way using ajax it will be very helpful to me. Thx! – user3731023 Dec 09 '14 at 20:45
  • You should've written `ajax` in your question, then. You wrote "I am sending a list of dates ..." - and that `for` loop in your template got me confused. Okay, will update my answer when I log into my desktop. Currently using phone. – xyres Dec 09 '14 at 22:24