How to use DatetimeField in Django 2 models and Templates












0















I'm working on a project in which I need to use a few DateTime fields, I have defined DatetimeField in my model and then in the template, I'm using the https://tempusdominus.github.io/bootstrap-4/ plugin but when I submit the form there are two issues comes on:



1): Django says Enter a valid date/time for all DateTime Fields
2): Select a valid choice. ['corn_oil'] is not one of the available choices.



Here's what I have tried:



From models.py:



choices = (
('CO2 SCRUBBER', 'CO2 SCRUBBER'),
('corn_oil', 'CORN OIL'),
('DRYERS', 'DRYERS'),
('ENVIRONMENTAL', 'ENVIRONMENTAL'),
('UTILITIES', 'UTILITIES'),
('LAB', 'LAB'),
('SIEVES', 'SIEVES'),
('GRAINS & MILLING', 'GRAINS & MILLING'),
('SEPARATION', 'SEPARATION'),
('AIR & GAS', 'AIR & GAS'),
('COOK', 'COOK'),
('EVAPORATION', 'EVAPORATION'),
('WATER', 'WATER'),
('STORAGE', 'STORAGE'),
('BOILER', 'BOILER'),
('FERMENTATION', 'FERMENTATION'),
('DISTILLATION', 'DISTILLATION'),
('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
('CHEMICAL', 'CHEMICAL'),
)


class ExperimentModel(models.Model):
user = models.ForeignKey(User, related_name='experiments',
on_delete=models.CASCADE)
name = models.CharField(max_length=255)
start_date = models.DateTimeField()
change_date = models.DateTimeField()
end_date = models.DateTimeField()
assets = models.CharField(max_length=450, choices=choices)
goals = models.CharField(max_length=255, blank=True)
comments = models.TextField(max_length=1000)
created_at = models.DateTimeField(auto_now=True)


From forms.py:



class ExperimentForm(forms.ModelForm):
choices = (
('CO2 SCRUBBER', 'CO2 SCRUBBER'),
('corn_oil', 'CORN OIL'),
('DRYERS', 'DRYERS'),
('ENVIRONMENTAL', 'ENVIRONMENTAL'),
('UTILITIES', 'UTILITIES'),
('LAB', 'LAB'),
('SIEVES', 'SIEVES'),
('GRAINS & MILLING', 'GRAINS & MILLING'),
('SEPARATION', 'SEPARATION'),
('AIR & GAS', 'AIR & GAS'),
('COOK', 'COOK'),
('EVAPORATION', 'EVAPORATION'),
('WATER', 'WATER'),
('STORAGE', 'STORAGE'),
('BOILER', 'BOILER'),
('FERMENTATION', 'FERMENTATION'),
('DISTILLATION', 'DISTILLATION'),
('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
('CHEMICAL', 'CHEMICAL'),
)
assets = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=choices)
# assets = ModelMultipleChoiceField(queryset=Thing.objects.all(), widget=Select2MultipleWidget)

class Meta:
model = ExperimentModel
fields = ('user', 'name', 'start_date', 'change_date', 'end_date', 'assets',
'goals', 'comments')


From views.py:
def post(self, request, *args, **kwargs):
post_data = request.POST.copy()
post_data.update({'user': request.user.pk})
form = ExperimentForm(post_data)
if request.method == 'POST':
print('req submitted')
if form.is_valid():
print('form valid')
form.save(commit=False)
form.user = request.user
form.save()
return HttpResponseRedirect('all-experiments')
return render_to_response('experiment/new_experiment.html', {'form': form})



And here how I'm displaying the DateTime field and choices field in my template:
From new-experiment.html:



<div class="form-group">
<input type="text" name="name" id="name" class="form-control input-lg"
placeholder="experiment name" tabindex="3" required>
</div>
<div class="form-group">
<label for="datetimepicker1"> Start Date </label>
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input type="text" name="start_date" class="form-control datetimepicker-input"
data-target="#datetimepicker1"/>
<div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div class="form-group">
<label for="datetimepicker2"> Change Date </label>
<div class="input-group date" id="datetimepicker2" data-target-input="nearest">
<input type="text" name="change_date" class="form-control datetimepicker-input"
data-target="#datetimepicker2"/>
<div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div class="form-group">
<label for="datetimepicker3"> End Date </label>
<div class="input-group date" id="datetimepicker3" data-target-input="nearest">
<input type="text" name="end_date" class="form-control datetimepicker-input"
data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
<div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>

<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
$('#datetimepicker2').datetimepicker();
$('#datetimepicker3').datetimepicker();
});
</script>
<div class="form-group">
<label for="assets"> Assets </label>
<input type="text" name="assets" id="assets" class="form-control input-lg"
placeholder="Assets" tabindex="3" required>
<select name="assets" class="form-control select_field_class" id="assets">
<option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
<option value="corn_oil">CORN OIL</option>
<option value="DRYERS">DRYERS</option>
<option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
<option value="UTILITIES">UTILITIES</option>
<option value="LAB">LAB</option>
<option value="SIEVES">SIEVES</option>
<option value="GRAINS & MILLING">GRAINS & MILLING</option>
<option value="SEPARATION">SEPARATION</option>
<option value="AIR & GAS">AIR & GAS</option>
<option value="COOK">COOK</option>
<option value="EVAPORATION">EVAPORATION</option>
<option value="WATER">WATER</option>
<option value="STORAGE">STORAGE</option>
<option value="BOILER">BOILER</option>
<option value="FERMENTATION">FERMENTATION</option>
<option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
<option value="CHEMICAL">CHEMICAL</option>
</select>
</div>
<div class="form-group">
<label for="assets"> Goals </label>
<input type="text" name="goals" id="goals" class="form-control input-lg"
placeholder="Goals" tabindex="3" required>
</div>
<div class="form-group">
<label for="comments"> Comments </label>
<textarea name="comments" id="comments" class="form-control input-lg"
rows="5" required>
</textarea>
</div>


I want to use multi-select for assets also, but not achieved yet.



Helm me, please!



Thanks in advance!










share|improve this question





























    0















    I'm working on a project in which I need to use a few DateTime fields, I have defined DatetimeField in my model and then in the template, I'm using the https://tempusdominus.github.io/bootstrap-4/ plugin but when I submit the form there are two issues comes on:



    1): Django says Enter a valid date/time for all DateTime Fields
    2): Select a valid choice. ['corn_oil'] is not one of the available choices.



    Here's what I have tried:



    From models.py:



    choices = (
    ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
    ('corn_oil', 'CORN OIL'),
    ('DRYERS', 'DRYERS'),
    ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
    ('UTILITIES', 'UTILITIES'),
    ('LAB', 'LAB'),
    ('SIEVES', 'SIEVES'),
    ('GRAINS & MILLING', 'GRAINS & MILLING'),
    ('SEPARATION', 'SEPARATION'),
    ('AIR & GAS', 'AIR & GAS'),
    ('COOK', 'COOK'),
    ('EVAPORATION', 'EVAPORATION'),
    ('WATER', 'WATER'),
    ('STORAGE', 'STORAGE'),
    ('BOILER', 'BOILER'),
    ('FERMENTATION', 'FERMENTATION'),
    ('DISTILLATION', 'DISTILLATION'),
    ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
    ('CHEMICAL', 'CHEMICAL'),
    )


    class ExperimentModel(models.Model):
    user = models.ForeignKey(User, related_name='experiments',
    on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    start_date = models.DateTimeField()
    change_date = models.DateTimeField()
    end_date = models.DateTimeField()
    assets = models.CharField(max_length=450, choices=choices)
    goals = models.CharField(max_length=255, blank=True)
    comments = models.TextField(max_length=1000)
    created_at = models.DateTimeField(auto_now=True)


    From forms.py:



    class ExperimentForm(forms.ModelForm):
    choices = (
    ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
    ('corn_oil', 'CORN OIL'),
    ('DRYERS', 'DRYERS'),
    ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
    ('UTILITIES', 'UTILITIES'),
    ('LAB', 'LAB'),
    ('SIEVES', 'SIEVES'),
    ('GRAINS & MILLING', 'GRAINS & MILLING'),
    ('SEPARATION', 'SEPARATION'),
    ('AIR & GAS', 'AIR & GAS'),
    ('COOK', 'COOK'),
    ('EVAPORATION', 'EVAPORATION'),
    ('WATER', 'WATER'),
    ('STORAGE', 'STORAGE'),
    ('BOILER', 'BOILER'),
    ('FERMENTATION', 'FERMENTATION'),
    ('DISTILLATION', 'DISTILLATION'),
    ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
    ('CHEMICAL', 'CHEMICAL'),
    )
    assets = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
    choices=choices)
    # assets = ModelMultipleChoiceField(queryset=Thing.objects.all(), widget=Select2MultipleWidget)

    class Meta:
    model = ExperimentModel
    fields = ('user', 'name', 'start_date', 'change_date', 'end_date', 'assets',
    'goals', 'comments')


    From views.py:
    def post(self, request, *args, **kwargs):
    post_data = request.POST.copy()
    post_data.update({'user': request.user.pk})
    form = ExperimentForm(post_data)
    if request.method == 'POST':
    print('req submitted')
    if form.is_valid():
    print('form valid')
    form.save(commit=False)
    form.user = request.user
    form.save()
    return HttpResponseRedirect('all-experiments')
    return render_to_response('experiment/new_experiment.html', {'form': form})



    And here how I'm displaying the DateTime field and choices field in my template:
    From new-experiment.html:



    <div class="form-group">
    <input type="text" name="name" id="name" class="form-control input-lg"
    placeholder="experiment name" tabindex="3" required>
    </div>
    <div class="form-group">
    <label for="datetimepicker1"> Start Date </label>
    <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
    <input type="text" name="start_date" class="form-control datetimepicker-input"
    data-target="#datetimepicker1"/>
    <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
    <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
    </div>
    </div>
    <div class="form-group">
    <label for="datetimepicker2"> Change Date </label>
    <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
    <input type="text" name="change_date" class="form-control datetimepicker-input"
    data-target="#datetimepicker2"/>
    <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
    <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
    </div>
    </div>
    <div class="form-group">
    <label for="datetimepicker3"> End Date </label>
    <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
    <input type="text" name="end_date" class="form-control datetimepicker-input"
    data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
    <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
    <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
    </div>
    </div>

    <script type="text/javascript">
    $(function () {
    $('#datetimepicker1').datetimepicker();
    $('#datetimepicker2').datetimepicker();
    $('#datetimepicker3').datetimepicker();
    });
    </script>
    <div class="form-group">
    <label for="assets"> Assets </label>
    <input type="text" name="assets" id="assets" class="form-control input-lg"
    placeholder="Assets" tabindex="3" required>
    <select name="assets" class="form-control select_field_class" id="assets">
    <option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
    <option value="corn_oil">CORN OIL</option>
    <option value="DRYERS">DRYERS</option>
    <option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
    <option value="UTILITIES">UTILITIES</option>
    <option value="LAB">LAB</option>
    <option value="SIEVES">SIEVES</option>
    <option value="GRAINS & MILLING">GRAINS & MILLING</option>
    <option value="SEPARATION">SEPARATION</option>
    <option value="AIR & GAS">AIR & GAS</option>
    <option value="COOK">COOK</option>
    <option value="EVAPORATION">EVAPORATION</option>
    <option value="WATER">WATER</option>
    <option value="STORAGE">STORAGE</option>
    <option value="BOILER">BOILER</option>
    <option value="FERMENTATION">FERMENTATION</option>
    <option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
    <option value="CHEMICAL">CHEMICAL</option>
    </select>
    </div>
    <div class="form-group">
    <label for="assets"> Goals </label>
    <input type="text" name="goals" id="goals" class="form-control input-lg"
    placeholder="Goals" tabindex="3" required>
    </div>
    <div class="form-group">
    <label for="comments"> Comments </label>
    <textarea name="comments" id="comments" class="form-control input-lg"
    rows="5" required>
    </textarea>
    </div>


    I want to use multi-select for assets also, but not achieved yet.



    Helm me, please!



    Thanks in advance!










    share|improve this question



























      0












      0








      0








      I'm working on a project in which I need to use a few DateTime fields, I have defined DatetimeField in my model and then in the template, I'm using the https://tempusdominus.github.io/bootstrap-4/ plugin but when I submit the form there are two issues comes on:



      1): Django says Enter a valid date/time for all DateTime Fields
      2): Select a valid choice. ['corn_oil'] is not one of the available choices.



      Here's what I have tried:



      From models.py:



      choices = (
      ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
      ('corn_oil', 'CORN OIL'),
      ('DRYERS', 'DRYERS'),
      ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
      ('UTILITIES', 'UTILITIES'),
      ('LAB', 'LAB'),
      ('SIEVES', 'SIEVES'),
      ('GRAINS & MILLING', 'GRAINS & MILLING'),
      ('SEPARATION', 'SEPARATION'),
      ('AIR & GAS', 'AIR & GAS'),
      ('COOK', 'COOK'),
      ('EVAPORATION', 'EVAPORATION'),
      ('WATER', 'WATER'),
      ('STORAGE', 'STORAGE'),
      ('BOILER', 'BOILER'),
      ('FERMENTATION', 'FERMENTATION'),
      ('DISTILLATION', 'DISTILLATION'),
      ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
      ('CHEMICAL', 'CHEMICAL'),
      )


      class ExperimentModel(models.Model):
      user = models.ForeignKey(User, related_name='experiments',
      on_delete=models.CASCADE)
      name = models.CharField(max_length=255)
      start_date = models.DateTimeField()
      change_date = models.DateTimeField()
      end_date = models.DateTimeField()
      assets = models.CharField(max_length=450, choices=choices)
      goals = models.CharField(max_length=255, blank=True)
      comments = models.TextField(max_length=1000)
      created_at = models.DateTimeField(auto_now=True)


      From forms.py:



      class ExperimentForm(forms.ModelForm):
      choices = (
      ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
      ('corn_oil', 'CORN OIL'),
      ('DRYERS', 'DRYERS'),
      ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
      ('UTILITIES', 'UTILITIES'),
      ('LAB', 'LAB'),
      ('SIEVES', 'SIEVES'),
      ('GRAINS & MILLING', 'GRAINS & MILLING'),
      ('SEPARATION', 'SEPARATION'),
      ('AIR & GAS', 'AIR & GAS'),
      ('COOK', 'COOK'),
      ('EVAPORATION', 'EVAPORATION'),
      ('WATER', 'WATER'),
      ('STORAGE', 'STORAGE'),
      ('BOILER', 'BOILER'),
      ('FERMENTATION', 'FERMENTATION'),
      ('DISTILLATION', 'DISTILLATION'),
      ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
      ('CHEMICAL', 'CHEMICAL'),
      )
      assets = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
      choices=choices)
      # assets = ModelMultipleChoiceField(queryset=Thing.objects.all(), widget=Select2MultipleWidget)

      class Meta:
      model = ExperimentModel
      fields = ('user', 'name', 'start_date', 'change_date', 'end_date', 'assets',
      'goals', 'comments')


      From views.py:
      def post(self, request, *args, **kwargs):
      post_data = request.POST.copy()
      post_data.update({'user': request.user.pk})
      form = ExperimentForm(post_data)
      if request.method == 'POST':
      print('req submitted')
      if form.is_valid():
      print('form valid')
      form.save(commit=False)
      form.user = request.user
      form.save()
      return HttpResponseRedirect('all-experiments')
      return render_to_response('experiment/new_experiment.html', {'form': form})



      And here how I'm displaying the DateTime field and choices field in my template:
      From new-experiment.html:



      <div class="form-group">
      <input type="text" name="name" id="name" class="form-control input-lg"
      placeholder="experiment name" tabindex="3" required>
      </div>
      <div class="form-group">
      <label for="datetimepicker1"> Start Date </label>
      <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
      <input type="text" name="start_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker1"/>
      <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>
      <div class="form-group">
      <label for="datetimepicker2"> Change Date </label>
      <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
      <input type="text" name="change_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker2"/>
      <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>
      <div class="form-group">
      <label for="datetimepicker3"> End Date </label>
      <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
      <input type="text" name="end_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
      <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>

      <script type="text/javascript">
      $(function () {
      $('#datetimepicker1').datetimepicker();
      $('#datetimepicker2').datetimepicker();
      $('#datetimepicker3').datetimepicker();
      });
      </script>
      <div class="form-group">
      <label for="assets"> Assets </label>
      <input type="text" name="assets" id="assets" class="form-control input-lg"
      placeholder="Assets" tabindex="3" required>
      <select name="assets" class="form-control select_field_class" id="assets">
      <option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
      <option value="corn_oil">CORN OIL</option>
      <option value="DRYERS">DRYERS</option>
      <option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
      <option value="UTILITIES">UTILITIES</option>
      <option value="LAB">LAB</option>
      <option value="SIEVES">SIEVES</option>
      <option value="GRAINS & MILLING">GRAINS & MILLING</option>
      <option value="SEPARATION">SEPARATION</option>
      <option value="AIR & GAS">AIR & GAS</option>
      <option value="COOK">COOK</option>
      <option value="EVAPORATION">EVAPORATION</option>
      <option value="WATER">WATER</option>
      <option value="STORAGE">STORAGE</option>
      <option value="BOILER">BOILER</option>
      <option value="FERMENTATION">FERMENTATION</option>
      <option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
      <option value="CHEMICAL">CHEMICAL</option>
      </select>
      </div>
      <div class="form-group">
      <label for="assets"> Goals </label>
      <input type="text" name="goals" id="goals" class="form-control input-lg"
      placeholder="Goals" tabindex="3" required>
      </div>
      <div class="form-group">
      <label for="comments"> Comments </label>
      <textarea name="comments" id="comments" class="form-control input-lg"
      rows="5" required>
      </textarea>
      </div>


      I want to use multi-select for assets also, but not achieved yet.



      Helm me, please!



      Thanks in advance!










      share|improve this question
















      I'm working on a project in which I need to use a few DateTime fields, I have defined DatetimeField in my model and then in the template, I'm using the https://tempusdominus.github.io/bootstrap-4/ plugin but when I submit the form there are two issues comes on:



      1): Django says Enter a valid date/time for all DateTime Fields
      2): Select a valid choice. ['corn_oil'] is not one of the available choices.



      Here's what I have tried:



      From models.py:



      choices = (
      ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
      ('corn_oil', 'CORN OIL'),
      ('DRYERS', 'DRYERS'),
      ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
      ('UTILITIES', 'UTILITIES'),
      ('LAB', 'LAB'),
      ('SIEVES', 'SIEVES'),
      ('GRAINS & MILLING', 'GRAINS & MILLING'),
      ('SEPARATION', 'SEPARATION'),
      ('AIR & GAS', 'AIR & GAS'),
      ('COOK', 'COOK'),
      ('EVAPORATION', 'EVAPORATION'),
      ('WATER', 'WATER'),
      ('STORAGE', 'STORAGE'),
      ('BOILER', 'BOILER'),
      ('FERMENTATION', 'FERMENTATION'),
      ('DISTILLATION', 'DISTILLATION'),
      ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
      ('CHEMICAL', 'CHEMICAL'),
      )


      class ExperimentModel(models.Model):
      user = models.ForeignKey(User, related_name='experiments',
      on_delete=models.CASCADE)
      name = models.CharField(max_length=255)
      start_date = models.DateTimeField()
      change_date = models.DateTimeField()
      end_date = models.DateTimeField()
      assets = models.CharField(max_length=450, choices=choices)
      goals = models.CharField(max_length=255, blank=True)
      comments = models.TextField(max_length=1000)
      created_at = models.DateTimeField(auto_now=True)


      From forms.py:



      class ExperimentForm(forms.ModelForm):
      choices = (
      ('CO2 SCRUBBER', 'CO2 SCRUBBER'),
      ('corn_oil', 'CORN OIL'),
      ('DRYERS', 'DRYERS'),
      ('ENVIRONMENTAL', 'ENVIRONMENTAL'),
      ('UTILITIES', 'UTILITIES'),
      ('LAB', 'LAB'),
      ('SIEVES', 'SIEVES'),
      ('GRAINS & MILLING', 'GRAINS & MILLING'),
      ('SEPARATION', 'SEPARATION'),
      ('AIR & GAS', 'AIR & GAS'),
      ('COOK', 'COOK'),
      ('EVAPORATION', 'EVAPORATION'),
      ('WATER', 'WATER'),
      ('STORAGE', 'STORAGE'),
      ('BOILER', 'BOILER'),
      ('FERMENTATION', 'FERMENTATION'),
      ('DISTILLATION', 'DISTILLATION'),
      ('BUILDING AND FACILITIES', 'BUILDING AND FACILITIES'),
      ('CHEMICAL', 'CHEMICAL'),
      )
      assets = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
      choices=choices)
      # assets = ModelMultipleChoiceField(queryset=Thing.objects.all(), widget=Select2MultipleWidget)

      class Meta:
      model = ExperimentModel
      fields = ('user', 'name', 'start_date', 'change_date', 'end_date', 'assets',
      'goals', 'comments')


      From views.py:
      def post(self, request, *args, **kwargs):
      post_data = request.POST.copy()
      post_data.update({'user': request.user.pk})
      form = ExperimentForm(post_data)
      if request.method == 'POST':
      print('req submitted')
      if form.is_valid():
      print('form valid')
      form.save(commit=False)
      form.user = request.user
      form.save()
      return HttpResponseRedirect('all-experiments')
      return render_to_response('experiment/new_experiment.html', {'form': form})



      And here how I'm displaying the DateTime field and choices field in my template:
      From new-experiment.html:



      <div class="form-group">
      <input type="text" name="name" id="name" class="form-control input-lg"
      placeholder="experiment name" tabindex="3" required>
      </div>
      <div class="form-group">
      <label for="datetimepicker1"> Start Date </label>
      <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
      <input type="text" name="start_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker1"/>
      <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>
      <div class="form-group">
      <label for="datetimepicker2"> Change Date </label>
      <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
      <input type="text" name="change_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker2"/>
      <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>
      <div class="form-group">
      <label for="datetimepicker3"> End Date </label>
      <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
      <input type="text" name="end_date" class="form-control datetimepicker-input"
      data-target="#datetimepicker3" placeholder="dd/mm/yy"/>
      <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
      </div>
      </div>

      <script type="text/javascript">
      $(function () {
      $('#datetimepicker1').datetimepicker();
      $('#datetimepicker2').datetimepicker();
      $('#datetimepicker3').datetimepicker();
      });
      </script>
      <div class="form-group">
      <label for="assets"> Assets </label>
      <input type="text" name="assets" id="assets" class="form-control input-lg"
      placeholder="Assets" tabindex="3" required>
      <select name="assets" class="form-control select_field_class" id="assets">
      <option value="CO2 SCRUBBER">CO2 SCRUBBER</option>
      <option value="corn_oil">CORN OIL</option>
      <option value="DRYERS">DRYERS</option>
      <option value="ENVIRONMENTAL">ENVIRONMENTAL</option>
      <option value="UTILITIES">UTILITIES</option>
      <option value="LAB">LAB</option>
      <option value="SIEVES">SIEVES</option>
      <option value="GRAINS & MILLING">GRAINS & MILLING</option>
      <option value="SEPARATION">SEPARATION</option>
      <option value="AIR & GAS">AIR & GAS</option>
      <option value="COOK">COOK</option>
      <option value="EVAPORATION">EVAPORATION</option>
      <option value="WATER">WATER</option>
      <option value="STORAGE">STORAGE</option>
      <option value="BOILER">BOILER</option>
      <option value="FERMENTATION">FERMENTATION</option>
      <option value="BUILDING AND FACILITIES">BUILDING AND FACILITIES</option>
      <option value="CHEMICAL">CHEMICAL</option>
      </select>
      </div>
      <div class="form-group">
      <label for="assets"> Goals </label>
      <input type="text" name="goals" id="goals" class="form-control input-lg"
      placeholder="Goals" tabindex="3" required>
      </div>
      <div class="form-group">
      <label for="comments"> Comments </label>
      <textarea name="comments" id="comments" class="form-control input-lg"
      rows="5" required>
      </textarea>
      </div>


      I want to use multi-select for assets also, but not achieved yet.



      Helm me, please!



      Thanks in advance!







      python django python-3.x django-models django-templates






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 9:36







      Abdul Rehman

















      asked Jan 19 at 6:17









      Abdul RehmanAbdul Rehman

      972323




      972323
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264592%2fhow-to-use-datetimefield-in-django-2-models-and-templates%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264592%2fhow-to-use-datetimefield-in-django-2-models-and-templates%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage