如何在Django 2模型和模板中使用DatetimeField

时间:2019-01-19 06:17:50

标签: python django python-3.x django-models django-templates

我正在一个项目中,我需要使用一些DateTime字段,我在模型中定义了exec,然后在模板中定义了https://tempusdominus.github.io/bootstrap-4/插件,但是当我提交表单时,会出现两个问题:

1):Django对所有DateTime字段说DatetimeField 2):选择一个有效的选择。 Enter a valid date/time不是可用的选择之一。

这是我尝试过的:

来自['corn_oil']

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)

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')     def post(self,request,* args,** kwargs):         post_data = request.POST.copy()         post_data.update({'user':request.user.pk})         表格= ExperimentForm(post_data)         如果request.method =='POST':             打印(“需要提交”)             如果form.is_valid():                 打印(“表格有效”)                 form.save(commit = False)                 form.user = request.user                 form.save()                 返回HttpResponseRedirect('all-experiments')             return render_to_response('experiment / new_experiment.html',{'form':form})

以下是我如何在模板中显示DateTime字段和options字段的信息: 来自views.py

new-experiment.html

我也想对资产使用多选功能,但尚未实现。

请帮我!

谢谢!

1 个答案:

答案 0 :(得分:0)

import datetime

class ExperimentModel(models.Model):
    start_date = models.DateTimeField(auto_now_add=True, blank=True)
    change_date = models.DateTimeField(blank=True)
    end_date = models.DateTimeField(blank=True)