将变量传递给表单以在django中显示

时间:2013-05-09 12:33:04

标签: django django-models django-forms django-templates django-views

views.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))

forms.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')
         }

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

由于

2 个答案:

答案 0 :(得分:0)

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

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。也许视图不是放置此代码的最佳位置。创建单独的函数来处理这个问题要好得多。

答案 1 :(得分: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))

尝试手动设置实例化时已格式化的表单的初始值。

希望它有所帮助!