0
views.pyQuering日期和時間字段並呈現模板
def new_report(request):
user = request.user
reports = Report.objects.filter(user=user)
today = datetime.datetime.today()
reports_today = reports.filter(created_date_time__year=today.year, created_date_time__month=today.month, created_date_time__day=today.day)
num_today = len(reports_today) + 1
num_today = str(num_today).zfill(3)
reportform = ReportForm()
if request.method == 'POST':
reportform = ReportForm(request.POST)
if reportform.is_valid():
report = reportform.save(commit=False)
report.user = user
report.created_date_time = today
return render(request, 'incident/new_report.html',
{
'newreport_menu': True,
'reports': reports,
'reportform':ReportForm,
})
models.py
class Report(models.Model):
user = models.ForeignKey(User, null=False)
incident_number = models.CharField('Incident Number', max_length=100)
device_id = models.CharField('Device Id', max_length=100)
app_uuid = models.CharField('Unique App Id', max_length=100)
created_date_time = models.DateTimeField('Created',auto_now=True)
模板
{{ reports.created_date_time|date:"j M Y g:i A" }}
擷取來自數據庫created_date_time並轉換爲這格式20 Jan2011 at 2:26PM
在模板中顯示。
查詢從數據庫created_date_time並顯示到template.I沒有得到任何錯誤的代碼,我認爲一些邏輯有問題,所以它不會發生。
看起來像'reports'變量是報告對象的列表,但你訪問它的模板它是一個單獨的Report對象。試試這個:'{{reports.0.created_date_time | date:「j MY g:i A」}}' – stalk 2013-05-04 13:36:45
ya,它顯示日期和時間,但是以這種格式2013年5月4日10:00 PM,所以我需要在2013年5月4日的晚上10點格式,即在日期和時間之間的字符串「at」應該包括,如何做到這一點。 – user2086641 2013-05-04 13:45:35
對此有何更新? – user2086641 2013-05-04 14:14:14