python 使用 SelectDateWidget 的 Django 表单字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/810308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:54:07  来源:igfitidea点击:

Django form field using SelectDateWidget

pythondjangoformswidgetadmin

提问by timbonicus

I've installed the latest SVN branch from Django which includes the new forms. I'm trying to use the SelectDateWidget from django.forms.extras.widgets but the field is showing up as a normal DateInput widget.

我已经从 Django 安装了最新的 SVN 分支,其中包括新的表单。我正在尝试使用 django.forms.extras.widgets 中的 SelectDateWidget,但该字段显示为普通的 DateInput 小部件。

Here is the forms.py from my application:

这是我的应用程序中的 forms.py:

from django import forms
from jacob_forms.models import Client

class ClientForm(forms.ModelForm):
    DOB = forms.DateField(widget=forms.extras.widgets.SelectDateWidget)

    class Meta:
            model = Client

What am I doing wrong? Checking the forms/extras/widgets.py I see the SelectDateWidget class exists.

我究竟做错了什么?检查 forms/extras/widgets.py 我看到 SelectDateWidget 类存在。

回答by timbonicus

The real problem was that SelectDateWidget can't be referenced this way. Changing the code to reference it differently solved my problem:

真正的问题是不能以这种方式引用 SelectDateWidget。更改代码以不同方式引用它解决了我的问题:

from django.forms import extras
...
    DOB = forms.DateField(widget=extras.SelectDateWidget)

This seems to be a limitation that you can't reference package.package.Class from an imported package. The solution imports extras so the reference is just package.Class.

这似乎是一个限制,您无法从导入的包中引用 package.package.Class。该解决方案导入额外内容,因此引用只是 package.Class。

回答by bchang

From the ticket re: the lack of documentation for SelectDateWidget here: Ticket #7437

来自票证:此处缺少 SelectDateWidget 的文档: 票证 #7437

It looks like you need to use it like this:

看起来你需要像这样使用它:

widget=forms.extras.widgets.SelectDateWidget()

Note the parentheses is the example.

请注意括号是示例。

回答by W.Perrin

Why not use forms.SelectDateWidget. Just use it as reference.

为什么不使用forms.SelectDateWidget. 仅用作参考。

import datetime

from django import forms


class HistDateForm(forms.Form):
    cur_year = datetime.datetime.today().year
    year_range = tuple([i for i in range(cur_year - 2, cur_year + 2)])
    hist_date = forms.DateField(initial=datetime.date.today() - datetime.timedelta(days=7),widget=forms.SelectDateWidget(years=year_range))

回答by rash

Here is the form.py

这是form.py

from django import forms
from django.forms import extras

DOY = ('1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987',
       '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995',
       '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003',
       '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011',
       '2012', '2013', '2014', '2015')


DOB = forms.DateField(widget=extras.SelectDateWidget(years = DOY))

回答by Jarret Hardie

Your code works fine for me as written. In a case like this, check for mismatches between the name of the field in the model and form (DOBversus dobis an easy typo to make), and that you've instantiated the right form in your view, and passed it to the template.

你的代码对我来说很好用。在这种情况下,检查模型和表单中的字段名称之间是否存在不匹配(DOBvsdob是一个容易犯的错误),并且您已经在视图中实例化了正确的表单,并将其传递给模板。