我是 Django 的新手,我一直在学习“Practical django Projects”这本书的框架(这本书教我们写一篇cms) 。我的代码运行正常,但下面的get_absolute_url
函数有时间问题。它实际上是在我的数据库中保存的时间提前8小时输出链接。我使用python shell来查看数据库中保存的时间和管理界面中保存的时间,它们都是正确的。但是,当我使用下面的get_absolute_url func
在浏览器中生成链接时,它会提前8小时关闭。我在Django设置文件中设置了正确的区域。我无法弄清楚出了什么问题。
我如何解决这个问题(我在我的数据库中使用sqlite3,Django 1.4.1)?
这是我的Entry类代码:
import datetime
from django.db import models
from django.contrib.auth.models import User
from tagging.fields import TagField
from markdown import markdown
from django.conf import settings
from django.utils.encoding import smart_str
class Entry(models.Model):
live = LiveEntryManager()
objects = models.Manager()
#define constant options
LIVE_STATUS = 1
DRAFT_STATUS = 2
HIDDEN_STATUS = 3
STATUS_CHOICES = (
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS,'Draft'),
(HIDDEN_STATUS, 'Hidden'),
)
#adding features to admin interface
class Meta:
ordering = ['-pub_date']
verbose_name_plural = "Entries"
#define model fields:
title = models.CharField(max_length=250)
excerpt = models.TextField(blank=True) #It's ok to not add anything for this field
body = models.TextField()
pub_date = models.DateTimeField(default=datetime.datetime.now())
slug = models.SlugField(unique_for_date='pub_date')
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False)
status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
#HTML
excerpt_html = models.TextField(editable=False, blank=True)
body_html = models.TextField(editable=False, blank=True)
#third party:
tag = TagField()
#relationship fields:
categories = models.ManyToManyField(Category)
author = models.ForeignKey(User)
#define methods:
def save(self, force_insert=False, force_update=False):#modify the model.save() method
self.body_html = markdown(self.body)
if self.excerpt:
self.excerpt_html = markdown(self.excerpt)#from excerpt field to excerpt_html
super(Entry, self).save(force_insert, force_update)
def get_absolute_url(self):
return "%s" % self.pub_date.strftime("year:%Y/day:%d/hour:%H/minute:%M/second:%S")
#@models.permalink
#def get_absolute_url(self):
#return ('coltrane_entry_detail', (), {'year': self.pub_date.strftime("%Y"),
'month': self.pub_date.strftime("%b").lower(),
'day': self.pub_date.strftime("%d"),
'slug': self.slug})
def __unicode__(self):
return self.title
这是我的entry_archive.html:
{% extends "base_entries.html"%}
{%block title%}{{block.super}} | Latest entries{% endblock %}
{% block content %}
{% for entry in latest %}
<h2>{{entry.title}}</h2>
<p>Published on {{ entry.pub_date|date:"F j P s, Y" }}</p>
{% if entry.excerpt_html%}
{{entry.excerpt_html|safe}}
{% else %}
{{entry.body_html|truncatewords_html:"50"|safe}}
{% endif%}
<p><a href="{{entry.get_absolute_url}}">Read full entry ...</a></p>
{% endfor %}
{%endblock%}
{%block whatis%}
<p>This is a list of the latest {{latest.count}} entries published in my blog.</p>
{% endblock %}
我无法发布截图,因为我是新用户。我的html中{{entry.pub_date | date:“F j P s,Y”}}给我正确的时间:12月28日上午11:24 am 2012.但是{{entry.get_absolute_url}}给了我一年:2012 /日:28 /小时:19 /分钟:24 /秒:45
我按照你们的建议将()添加到pub_date = models.DateTimeField(default = datetime.datetime.now()),但结果仍然相同(该书实际上建议不要添加())。令我烦恼的是,{{entry.pub_date | date:“F j P s,Y”}}给了我正确的时间,但{{entry.get_absolute_url}}提前8小时。我将我的setting.py时区设置为TIME_ZONE ='America / Los_Angeles'。感谢所有的快速反应,但这让我感到害怕......
答案 0 :(得分:0)
更改以下内容:
pub_date = models.DateTimeField(default=datetime.datetime.now)
到
pub_date = models.DateTimeField(default=datetime.datetime.now())
或
pub_date = models.DateTimeField(auto_now_add=True)