django localflavor USStateSelect()inital值 [英] django localflavor USStateSelect() inital value

查看:135
本文介绍了django localflavor USStateSelect()inital值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在窗体中使用django localflavor小部件USStateSelect(),但是我希望小部件默认选择内布拉斯加。小部件只接受一个attrs arg,所以我试图理解我必须设置什么属性以获得所需的结果。以下是我在forms.py中的内容:

I'm trying to use the django localflavor widget USStateSelect() in a form, but I want the widget to have Nebraska selected by default. The Widget only accepts an attrs arg, so I'm trying to understand what attributes I have to set to get the desired result. Here's what I've got in my forms.py:

state = forms.CharField(widget=USStateSelect(attrs={'value':'NE'}))

这是HTML select元素的文档: http://www.w3.org/html /wg/drafts/html/CR/forms.html#the-select-element

This is the docs for the HTML select element: http://www.w3.org/html/wg/drafts/html/CR/forms.html#the-select-element

这是localflavor的文档: https://django-localflavor.readthedocs.org/en/latest/_modules/localflavor/我们/表单/

This is the docs for localflavor: https://django-localflavor.readthedocs.org/en/latest/_modules/localflavor/us/forms/

<option value="NE" selected>Nebraska</option>

这是我需要在我的HTML中,但我无法弄清楚attrs dict需要包含才能实现这个结果。我已经尝试添加'selected':'selected'和'class':'selected'到dict,但是没有这样做。

This is what I need to have in my html, but I can't figure out what the attrs dict needs to contain to achieve this result. I've tried adding 'selected':'selected' and 'class':'selected' to the dict, but that's not doing it.

我看到一些人问如何添加一个空选项,但没有人似乎希望将其默认为特定状态。任何想法都是受欢迎的。

I've seen a number of people asking how to add an empty option, but no one seems to want to make it default to a specific state. Any ideas are welcome.

谢谢,

Anthony

推荐答案

您可以设置如下初始值:

You can set the initial value like so:

state = forms.CharField(widget=USStateSelect(), initial='NE')

或者您可以在实例化形式:

or you can set it when you instantiate the form:

form = ExampleForm(initial={'state': 'NE'})

这篇关于django localflavor USStateSelect()inital值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆