- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
首先,这个问题已经被问过here ,但是答案不起作用,并且也适用于 Django 1.3。在对 SO、Django Docs 和一般谷歌搜索上的类似问题进行了广泛的研究后,我仍然找不到有效的答案。这并不是一个重要的细节,但在尝试使用我正在创建的表单时以及因为我无法解决它时,它都很烦人。
在 ModelForm 中使用 SelectDateWidget 时,我希望能够将小部件上的默认值设置为今天的日期。
forms.py
from django import forms
from .models import Article
from django.utils import timezone
class ArticleForm(forms.ModelForm):
publish=forms.DateField(widget=forms.SelectDateWidget(), initial = timezone.now)
class Meta:
model = Article
fields = [
'title',
...
'publish',
]
models.py
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from Blog.models import Category
class Article(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length = 120, unique=True)
...
publish = models.DateField(default=timezone.now)
我假设我在某个地方有一些语法错误,但我并没有找到它。
最佳答案
在设置 DateTimeField
和 DateField
的默认时间时,看起来有细微的差别。
DateField.auto_now_add Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set the following instead of auto_now_add=True:
For DateField: default=date.today - from datetime.date.today() For DateTimeField: default=timezone.now - from django.utils.timezone.now()
因此,您需要使用 date.today
而不是 timezone.now
。
关于python - 使用 Django SelectDateWidget 设置默认日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934402/
在项目中使用了SelectDateWidget 并在模板中渲染它只需要编写form.date_of_sth,但它将所有小部件(选择)渲染为一个。我想分别渲染每一个。有什么办法吗? 最佳答案 在 Dja
我正在使用带有 django-registration 的 SelectDateWidget 在注册时保存出生日期。我尝试排除生日,并使用额外数据创建并保存配置文件。我不太确定哪里出了问题。 模型.p
有没有办法让“SelectDateWidget”以相反或 DESC 顺序显示年份? my_form_field = forms.DateField(widget=MonthYearWidget(yea
是否有不同的小部件或参数允许 django 仅显示/获取年和月输入,而不是年、月和日? 当前使用 SelectDateWidget。 最佳答案 有一个片段here ,它将日期设置为 1(假设您有一个
我正在尝试在 Calendarium 中创建自定义表单/ View ,但我一直收到错误 AttributeError: 'module' object has no attribute 'Select
我试图在一行上显示使用 Django SelectDateWidget 呈现的 3 个选择字段。当我使用脆皮表格时,它们都位于不同的行上。有没有办法使用布局助手来实现这一点? 谢谢! class Wi
我正在尝试在 Django 中呈现内置的 SelectDateWidget,但是在网页中它显示为 Submit 我是 Django 的新手,我阅读了文档并在 Internet 上查找,但找不到解决方
我的表单有一个非常具体的 HTML 设计,所以我需要它全部用 HTML 编写,而且我不使用默认的表单渲染器。 我在我的 Django 表单中设置了这个 SelectDateWidget。除了 Sele
我在 Django 的表单中使用 SelectDateWidget 作为生日字段。显示日、月、年三种选择。 问题是它们不是内联显示,而是垂直堆叠。 在本例中,我为每个字段使用了一个标签 {{ form
首先,这个问题已经被问过here ,但是答案不起作用,并且也适用于 Django 1.3。在对 SO、Django Docs 和一般谷歌搜索上的类似问题进行了广泛的研究后,我仍然找不到有效的答案。这并
我正在使用 SelectDateWidget用于在表单字段中输入日期的小部件。但我希望它默认显示当前日期。我怎样才能做到这一点? 模型.py bdate = models.DateField(defa
我想将 django 的 selectDateWidget 用于表单,但年份的选项是从 2012 年到 2021 年。我需要显示过去的年份,可能到 1980 年。如何修改 selectDateWidg
我在 forms.py 中设置了 ContactView,如下所示: from django.forms import ModelForm, extras from .models import Co
我已经从 Django 安装了最新的 SVN 分支,其中包括新的表单。我正在尝试使用 django.forms.extras.widgets 中的 SelectDateWidget,但该字段显示为普通
根据 Django 关于 SelectDateWidget ( https://docs.djangoproject.com/en/1.8/ref/forms/widgets/#selectdatew
这是表格: from django.forms.extras.widgets import SelectDateWidget class EntryForm(forms.ModelForm):
对于 SelectDateWidget,Django 文档说明如下: If the DateField is not required, SelectDateWidget will have an e
class PersonalForm(ModelForm): class Meta: model = InfoPersonal widgets = {'date
如何在 Django 中设置 SelectDateWidget 的 MaxDate 和 MinDate? 我正在将该小部件用于生日字段。 #found in: from django.forms.ex
我是一名优秀的程序员,十分优秀!