我想时区管理被添加到django 1.4,所以这个问题很新.
我用了一个简单的模型
class Sample(models.Model):
...
date_generated = models.DateTimeField(auto_now_add = True)
Run Code Online (Sandbox Code Playgroud)
当我尝试检索新创建的记录时,它失败了.
min_datetime = datetime.now() - timedelta(seconds = 300)
sample = Sample.objects.get(date_generated__gte = min_datetime)
Run Code Online (Sandbox Code Playgroud)
并且服务器发出警告.
DateTimeField received a naive datetime (2012-06-29 15:02:15.074000) while time zone support is active.
Run Code Online (Sandbox Code Playgroud)
我找到了解决这个问题的两个方案.
在settings.py中禁用时区管理
USE_TZ = False
Run Code Online (Sandbox Code Playgroud)
但这并不总是令人满意的.
改变
date_generated = models.DateTimeField(auto_now_add = True)
Run Code Online (Sandbox Code Playgroud)
至
date_generated = models.DateTimeField(default=datetime.now())
Run Code Online (Sandbox Code Playgroud)
是时区管理工作的解决方案
我有以下代码:
digraph G {
bgcolor=antiquewhite;
compound=true;
{
rankdir=LR ;
rank=same g0 p1 p2 p3 h1;
}
subgraph cluster0 {
style=filled;
color=khaki;
g0 [label="G",shape=circle,style="filled", color="red", fillcolor="lightpink"]
label = "Cluster 0";
g0 -> p1;
}
subgraph cluster1 {
style=filled;
color=khaki;
p1 [label="S2",shape=box,style="filled", color="blue", fillcolor="skyblue"];
p2 [label="S3",shape=box,style="filled", color="blue", fillcolor="skyblue"];
p3 [label="S3",shape=box,style="filled", color="blue", fillcolor="skyblue"];
label = "Cluster 1";
p1 -> p2 -> p3 [arrowhead=none] ;
}
subgraph cluster2 {
style=filled;
color=khaki;
h1 [label="h1",shape=box,style="invis"];
label = "Cluster 2";
p3 -> h1;
}
}
Run Code Online (Sandbox Code Playgroud)
除了子图不显示外,一切都完美无缺。一旦在集群外定义了等级,子图就会消失。

如果在簇体内部定义,则簇之间的相同等级将丢失。
我尝试了通用视图的简约django实现来上传个人资料图片.
views.py
class UpdateProfile(UpdateView):
form_class = UpdateUserProfileForm
model = UserProfile
success_url = reverse_lazy('show_profile')
Run Code Online (Sandbox Code Playgroud)
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True)
picture = models.ImageField(upload_to='user/img/%Y-%m-%d/', blank=True)
Run Code Online (Sandbox Code Playgroud)
forms.py
class UpdateUserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['website','picture']
Run Code Online (Sandbox Code Playgroud)
userprofile_form.html
<form action="" enctype="multipart/form-data" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans "Save" %}"/>
</form>
Run Code Online (Sandbox Code Playgroud)
一切正常.现在错误消息.网站字段将正确更新,搜索按钮允许选择要上传的文件.但是,该文件永远不会出现在系统中,并且数据库字段仍为空.
不幸的是,关于文件上传的django文档(https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/)不包含通用视图,所以我想知道它是否可行.
更新:感谢Alasdair的回答,我更新了我的模板,因此它现在可以正常用作带有通用视图的图片上传的简约原型.
要显示图片,文档(https://docs.djangoproject.com/en/1.10/howto/static-files/)的说明再次非常有用.
此外,媒体设置是将文件上载到媒体文件夹所必需的.
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = 'absolute-path-to/media'
Run Code Online (Sandbox Code Playgroud)
urls.py
from django.conf import settings
from django.conf.urls.static import static …Run Code Online (Sandbox Code Playgroud)