0

views.py在Django傳遞變量的形式顯示

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

如何通過轉換後的日期和時間的格式,即,日期和時間變量,以形成。我在表單中指定的格式現在正在顯示,但取決於視圖上的條件,時間和日期格式應該在表單域中更改。

感謝

回答

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

這會嘗試手動設置當您實例化時已格式化的表單的初始值。

希望它有幫助!

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。也許該觀點並不是放置此代碼的最佳地點。創建單獨的函數來處理這個問題要好得多。

+0

更新了我的答案 – matino 2013-05-09 13:05:52