0

视图.py

def when(request):
    user = request.user
    report = Report.objects.get(user=request.user)
    reportform = ReportForm(instance=report)

    settings = Settings.objects.get(user=request.user)
    settingsForm = SettingsForm(instance=settings) 
 #   settings=Settings.objects.get(user=2)
    if settings.date_format == '0':
        date = report.manual_date.strftime('%d/%m/%Y')
    else:
        date = report.manual_date.strftime('%m/%d/%Y')
    if settings.time_format == '0':
        time = report.manual_time.strftime('%I:%M%p')
    else:
        time = report.manual_time.strftime('%H:%M')
    if request.method == 'POST':            
        reportform = ReportForm(instance=report,data=request.POST,initial={'manual_date': date,'manual_time': time})
        if reportform.is_valid():
            report = reportform.save(commit=False) 
            report.user = request.user
            report.save()
            return redirect('/member/media/')

    return render_to_response('incident/when.html',{
                  'newreport_menu': True, 
                  'form': reportform,
                   'date':date,
                   'time':time,                  
                   },
                   context_instance=RequestContext(request))

表格.py

class ReportForm(forms.ModelForm):   
    class Meta:
        model = Report
        fields = ['incident_description','manual_date','manual_time', 'location_description',
                  'incident_followup', 'incident_followup_name_1', 'incident_followup_email_1', 
                  'incident_followup_name_2', 'incident_followup_email_2', 'phone_call_log', 
                  'notes_other','notes_firstaid','notes_risk']
    #    manual_date = forms.DateField(input_formats=['%d/%m/%Y', '%d-%m-%Y'],
     #                              widget=forms.DateInput(format="%-d/%-m/%Y"))
        widgets = {'manual_date': forms.DateInput(attrs={'size':'15','id':'datepicker',
            },format='%d/%m/%Y'),'manual_time': forms.TimeInput(attrs={'size':'8','class':'time_field', },format='%H:%M')
         }

如何将转换后的日期和时间格式,即日期和时间变量传递给表单。我在表单中指定的格式现在正在显示,但取决于视图的条件,时间和日期格式应该在表单字段中更改。

谢谢

4

2 回答 2

0

您可以使用模板标签/过滤器日期。在视图级别定义它并传递给模板:

if settings.date_format == '0':
    date_filter = 'd/m/Y'
else:
    date_filter = 'm/d/Y'
if settings.time_format == '0':
    time_filter = 'I:Mp'
else:
    time_filter = 'H:M'

在模板中:

{{ form.manual_date|date:date_filter }}
{{ form.manual_time|date:time_filter }}

我不确定当前的格式,但您可以随时查看参考以获得您想要的。

PS。也许视图不是放置此代码的最佳位置。最好创建单独的函数来处理这个问题。

于 2013-05-09T12:46:13.763 回答
0

尝试使用此行创建表单:

reportform = ReportForm(instance=report,initial={'manual_date':date, 'manual_time':time})

其中date变量是格式化的日期。您将不得不向下移动表单的实例化。这将是您的代码:

def when(request):
    user = request.user
    report = Report.objects.get(user=request.user)

    settings = Settings.objects.get(user=request.user)
    settingsForm = SettingsForm(instance=settings) 
 #   settings=Settings.objects.get(user=2)
    if settings.date_format == '0':
        date = report.manual_date.strftime('%d/%m/%Y')
    else:
        date = report.manual_date.strftime('%m/%d/%Y')
    if settings.time_format == '0':
        time = report.manual_time.strftime('%I:%M%p')
    else:
        time = report.manual_time.strftime('%H:%M')
    if request.method == 'POST':            
        reportform = ReportForm(instance=report,data=request.POST,initial={'manual_date': date,'manual_time': time})
        if reportform.is_valid():
            report = reportform.save(commit=False) 
            report.user = request.user
            report.save()
            return redirect('/member/media/')

    reportform = ReportForm(instance=report,initial={'manual_date':date, 'manual_time':time})
    return render_to_response('incident/when.html',{
                  'newreport_menu': True, 
                  'form': reportform,
                   'date':date,
                   'time':time,                  
                   },
                   context_instance=RequestContext(request))

当您实例化它时,它会尝试手动设置已经格式化的表单的初始值。

希望能帮助到你!

于 2013-05-09T16:05:24.070 回答