from django import forms
from events.models import Event, City

class EventForm(forms.ModelForm):
   city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")

   class Meta:
        model = Event
        exclude = ('submission_time', 'updated_time', 'decision_time',
                   'moderator', 'moderated', 'latitude', 'longitude')


from django import forms
from events.models import Event, City
from django.forms.util import ErrorList
from datetime import datetime

class EventForm(forms.ModelForm):
    city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")

    class Meta:
      model = Event
      exclude = ('submission_time', 'updated_time', 'decision_time',
                 'moderator', 'moderated', 'latitude', 'longitude')

    def clean(self):
      cleaned_data = self.cleaned_data
      start_time = cleaned_data.get("start_time")
      end_time = cleaned_data.get("end_time")

      if start_time >= end_time:
        msg = u"L'évènement ne peut se terminer avant son début"
        self._errors["start_time"] = ErrorList([msg])
        self._errors["end_time"] = ErrorList([msg])

        del cleaned_data["start_time"]
        del cleaned_data["end_time"]

      elif start_time < datetime.today():
        msg = u"Seul les évènements à venir sont acceptés"
        self._errors["start_time"] = ErrorList([msg])

        del cleaned_data["start_time"]

      return cleaned_data

Validate entered dates in Event form
Validate entered dates in Event form

  from django import forms
  from events.models import Event, City
+ from django.forms.util import ErrorList
+ from datetime import datetime
  
  class EventForm(forms.ModelForm):
-    city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")
+     city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")
  
-    class Meta:
+     class Meta:
-         model = Event
+       model = Event
-         exclude = ('submission_time', 'updated_time', 'decision_time',
+       exclude = ('submission_time', 'updated_time', 'decision_time',
-                    'moderator', 'moderated', 'latitude', 'longitude')
+                  'moderator', 'moderated', 'latitude', 'longitude')
  
+     def clean(self):
+       cleaned_data = self.cleaned_data
+       start_time = cleaned_data.get("start_time")
+       end_time = cleaned_data.get("end_time")
  
+       if start_time >= end_time:
+         msg = u"L'évènement ne peut se terminer avant son début"
+         self._errors["start_time"] = ErrorList([msg])
+         self._errors["end_time"] = ErrorList([msg])
+ 
+         del cleaned_data["start_time"]
+         del cleaned_data["end_time"]
+ 
+       elif start_time < datetime.today():
+         msg = u"Seul les évènements à venir sont acceptés"
+         self._errors["start_time"] = ErrorList([msg])
+ 
+         del cleaned_data["start_time"]
+ 
+       return cleaned_data
+ 
Validate entered dates in Event form
## Code Before:
from django import forms
from events.models import Event, City

class EventForm(forms.ModelForm):
   city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")

   class Meta:
        model = Event
        exclude = ('submission_time', 'updated_time', 'decision_time',
                   'moderator', 'moderated', 'latitude', 'longitude')


## Instruction:
Validate entered dates in Event form
## Code After:
from django import forms
from events.models import Event, City
from django.forms.util import ErrorList
from datetime import datetime

class EventForm(forms.ModelForm):
    city = forms.ModelChoiceField(City.objects.all(), empty_label=None, label="Ville")

    class Meta:
      model = Event
      exclude = ('submission_time', 'updated_time', 'decision_time',
                 'moderator', 'moderated', 'latitude', 'longitude')

    def clean(self):
      cleaned_data = self.cleaned_data
      start_time = cleaned_data.get("start_time")
      end_time = cleaned_data.get("end_time")

      if start_time >= end_time:
        msg = u"L'évènement ne peut se terminer avant son début"
        self._errors["start_time"] = ErrorList([msg])
        self._errors["end_time"] = ErrorList([msg])

        del cleaned_data["start_time"]
        del cleaned_data["end_time"]

      elif start_time < datetime.today():
        msg = u"Seul les évènements à venir sont acceptés"
        self._errors["start_time"] = ErrorList([msg])

        del cleaned_data["start_time"]

      return cleaned_data
