[英]Django - How to call localflavor US_STATES in HTML
How do you make a drop down menu that contains all of the options in localflavor's US_STATES? 您如何制作一个包含localflavor的US_STATES中所有选项的下拉菜单?
I can see how to create a model that contains a field that uses the localflavor option US_STATES. 我可以看到如何创建一个包含使用localflavor选项US_STATES的字段的模型。
class State(models.Model):
states = models.CharField(max_length=2, choices=US_STATES , null=True, blank=True)
The field state
is then in a manytomany relationship to a model called Person
. 字段
state
与称为Person
的模型有许多关系。 How do you take this a put it in a html page? 您如何将其放在html页面中?
In my view, I can only think of doing this. 我认为,我只能考虑这样做。
def get_context_data(self, *args, **kwargs):
context = super(UserProfileUpdateView, self).get_context_data(*args, **kwargs)
context['states'] = State.objects.all()
But this only pulls the existing state options. 但这只会拉出现有的状态选项。
1) How do I pull all states into the view? 1)如何将所有状态拉入视图?
2) How can I render an html template to use the output of 1? 2)我如何渲染HTML模板以使用1的输出? I imagine it has something to do with the 'choices' option, but I haven't ever done that before.
我想它与“选择”选项有关,但是我以前从未这样做过。
Thanks 谢谢
You probably want to take a look at Django's forms system . 您可能想看看Django的forms系统 。 Since what you're doing here is rendering a form (I'm guessing that by "drop down menu" you mean an HTML
<select>
element with all the states as options), that would be the preferred way to do it. 由于您在这里正在渲染表单(我猜是通过“下拉菜单”,您是指一个HTML
<select>
元素,其中所有状态都为选项),因此这是首选的方式。
The localflavor package includes form field classes for working with its data types, including states. localflavor软件包包括用于处理其数据类型(包括状态)的表单字段类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.