在模型中我有一个DateTime字段。但在我的模板中,我有单独的日期和时间供用户输入。我如何将当前时间作为默认值。
Models.py
datetime_reading=models.DateTimeField()
truepower_reading=models.DecimalField(verbose_name="True Power Reading(KWH)",max_digits=6,decimal_places=2,blank=False,null=False)
apparentpower_reading=models.DecimalField(verbose_name="Apparent Power Reading(KVAH)",max_digits=6,decimal_places=2,blank=False,null=False)
truepower_consumed=models.DecimalField(max_digits=6,decimal_places=2,blank=False,null=False)
voltage_reading=models.IntegerField(blank=False,null=False)
powerfactor=models.DecimalField(max_digits=3,decimal_places=2)
Forms.py
Date=forms.DateField()
Time=forms.TimeField()
fields = ('date', 'time', 'truepower_reading', 'apparentpower_reading',"voltage_reading")
class Meta:
model=KEBReading
exclude=("truepower_consumed","powerfactor","datetime_reading")
widgets = {
"date" : forms.widgets.DateInput(format="%d/%m/%Y"),
"time" : forms.widgets.TimeInput(format="%H:%M")
}
views.py
def KEBReading1(request):
q2=KEBReading.objects.all().order_by('datetime_reading')[:30]
form = KEBReading_form()
monthform=MonthForm()
if request.method == "POST":
if request.POST.get('KEBsubmit'):
form = KEBReading_form(request.POST)
if form.is_valid():
try:
prevdate=KEBReading.objects.latest("datetime_reading")
date=form.cleaned_data["Date"]
print type(date)
time=form.cleaned_data["Time"]
print type(time)
kr_truepower_reading = form.cleaned_data["truepower_reading"]
kr_apparentpower_reading = form.cleaned_data["apparentpower_reading"]
truepower_consumed1=kr_truepower_reading-prevdate.truepower_reading
powerfactor1=((kr_truepower_reading-prevdate.truepower_reading)/(kr_apparentpower_reading-prevdate.apparentpower_reading))
form1=form.save(commit=False)
print "tee"
form1.datetime_reading=datetime.combine(date,time)
print form1.datetime_reading
form1.truepower_consumed=truepower_consumed1
form1.powerfactor=powerfactor1
print form1.powerfactor
form1.save()
这是我的观看代码。我试图从表格中获取输入的时间。但我是否给用户输入当前时间。
views.py
q2=KEBReading.objects.all().order_by('datetime_reading')[:30]
form = KEBReading_form()
monthform=MonthForm()
if request.method == "POST":
if request.POST.get('KEBsubmit'):
form = KEBReading_form(request.POST)
if form.is_valid():
try:
prevdate=KEBReading.objects.latest("datetime_reading")
time = datetime.datetime.now().strftime("%I")
data = {'Time':time}
form = KEBReading_form(data)
date=form.cleaned_data["Date"]
print type(date)
time=form.cleaned_data["Time"]
print type(time)
kr_truepower_reading = form.cleaned_data["truepower_reading"]
kr_apparentpower_reading = form.cleaned_data["apparentpower_reading"]
我在上面提到的观点中给出了它。我是否必须在表格中指定sumthin ???
我想要小时,分钟和secoond。这是对的
form = KEBReading_form()
time1 = datetime.datetime.now()
time=time1.hour,time1.minute,time1.second
data = {'Time':time}
print data
form = KEBReading_form(data)
答案 0 :(得分:2)
documentation描述了在表单中设置初始值。
<强> views.py 强>
def view_name(request):
q2=KEBReading.objects.all().order_by('datetime_reading')[:30]
time = datetime.datetime.now().strftime("%I:%M")
data = {'Time':time}
form = KEBReading_form(data)
monthform=MonthForm()
if request.method == "POST":
...........
return render(request, 'page.html', {'form':form,})