1

I have a Django form, with a date field displayed on it, which has a data type of mDateField (a custom class that extends forms.DateField). It is defined in forms.py with:

class mDateField(forms.DateField):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('input_formats', DATE_INPUT_FORMATS)
        super(mDateField, self).__init__(*args, **kwargs)

I now want to set a 'minimum' & 'maximum' possible value for that date field (i.e. a date a certain length of time either in the past or in the future relevant to today's date that this value can be set to).

I came across this answer on SO: jquery datetime picker set minDate dynamic and tried following what it says, by writing the line:

    presentation_date = mDateTimeField(required=False, widget=forms.DateTimeInput(format='%d/%m/%Y %H:%M', attrs=({'class':'datetimepicker presentation_date', 'name':'presentation_date2'})))
    presentation_date.options.maxDate = $( ".selector" ).datepicker( "option", "maxDate", new Date(2014, 1 - 1, 1));

but when I do, I get a syntax error in the console, which points to the $, and says:

SyntaxError: invalid syntax

What is wrong with what I'm doing here? How can I specify what the earliest/ latest date to be shown in the datepicker should be?

Edit In the jQuery file where datetimepicker is defined (jquery.datetimepicker.full.js), there are the following lines:

if (options.minDate && /^[\+\-](.*)$/.test(options.minDate)) {
    options.minDate = dateHelper.formatDate(_xdsoft_datetime.strToDateTime(options.minDate), options.formatDate);
}

if (options.maxDate &&  /^[\+\-](.*)$/.test(options.maxDate)) {
    options.maxDate = dateHelper.formatDate(_xdsoft_datetime.strToDateTime(options.maxDate), options.formatDate);
}

But I don't see how these are setting the values to any actual dates...?

Community
  • 1
  • 1
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • 1
    But.. that's jQuery code. Why are you putting that in the Python file? – Daniel Roseman Dec 14 '16 at 15:46
  • Ah, so I should be putting that in jQuery code somewhere? As I understand, my Python code is referencing an external jQuery library- I don't think I have any local jQuery code that's affecting the `mDateTimeField` at all... If I right- click on `datetimepicker` in my code, and 'Go to definition' it opens up the file jquery.datetimepicker.full.js, but as I understand, this is an external library, so I won't be able to change this? – Noble-Surfer Dec 14 '16 at 15:58

1 Answers1

1

Ok, firstly - Remove the javascript code from the python files ;). Second - Any validation of your django form must be done on the server(Django), not the front-end(JS)! So, there's two things you can do:
1) Writing custom widget and adding it to your datefield(That's harder).
2) Use django default DateField(https://docs.djangoproject.com/en/1.10/ref/forms/fields/#datefield):

presentation_date = forms.DateField()

If using default DateField, you can write your custom method in the form class, named (clean_your_field_name), in your case it will be called clean_presentation_date:

def clean_presentation_date(self, value):
    if value >= some date and value < another_date:
        raise form.ValidationError('The date must be ...')
    return value
  • The problem I'm having, is that the `mDateTimeField` that displays the `datetimepicker` on the form when the user clicks in that field will only displays dates between 01/01/2015- 01/01/2017 (as I scroll forward/ backward through the calendar that's displayed). Obviously as it's now approaching the 01/01/2017, I need to be able to schedule meetings for dates beyond the 01/01/2017, but all of the dates later than this in the calendar/ `datetimepicker` are greyed out, and it is not possible to select them. The field needs to be a `DateTime` value, rather than a `Date` value... – Noble-Surfer Dec 15 '16 at 09:09
  • Having searched on my computer for the file in which the `datetimepicker` is defined, I have found it located in project/static/js. When looking at the properties for the file, it states that the library was developed by krajee.com. I have looked through the documentation at http://plugins.krajee.com, but can't see any reference to a 'minimum' or 'maximum' date value. I'm wondering if maybe the plugin doesn't support this? But it seems strange that the only dates available for selection are within the last two years... Could it be that this has been hard-coded into the plugin? – Noble-Surfer Dec 15 '16 at 09:36
  • The javascript is not in the Python files- it's in the template (.html), and there is also a .js file that's 'included' by the template. – Noble-Surfer Dec 15 '16 at 16:38