如何在Django中创建一个包含表单的国家列表的选择框 [英] How to build a select box with a list of countries for a form in Django

查看:125
本文介绍了如何在Django中创建一个包含表单的国家列表的选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢django,我正在建立一个表单,我需要一个选择框,供用户选择他们的国家。我正在使用ChoiceField,我创建了一个单独的py文件与国家列表。我的布局是这样的:



表单文件:



从django import COUNTRIES #I有一个py文件与国家列表



country = forms.ChoiceField(COUNTRIES,label = u'Country')



我没有想到它会工作 - 我有一个错误说,它不能导入名称国家。我不知道为了实现我的目标采取什么步骤。任何有用的提示?

解决方案

您可以使用与非django python模块相同的方式导入模块。



您使用国家/地区列表制作的模块不是django模块,因此您不要将其从django导入。



如果你有一个文件,my_choices.py看起来像这样:



extras.py

  COUNTRY_CHOICES =(('USA','USA'),
('JPN','JAPAN'),
('CAN','CANADA'))

它位于项目的根目录中:

  myproject / 
| _ __init__.py
| _ my_choices.py
| _ settings.py
...

如果您的项目位于PYTHONPATH中,您可以输入以下内容来导入选项:

 >>>来自my_choices import COUNTRY_CHOICES 
>>>>打印COUNTRY_CHOICES
(('USA','USA'),('JPN','JAPAN'),('CAN','CANADA'))
>>>

我通常把这样的东西放在一个utils文件夹中:

  myproject / 
| _ __init__.py
| _ utils /
| _ __init__.py
| _ my_choices。 py

so:

 >>>来自utils.my_choices import COUNTRY_CHOICES 
>>> COUNTRY_CHOICES
(('USA','USA'),('JPN','JAPAN'),('CAN','CANADA'))
>>>您的forms.py中的



  from utils.my_choices import COUNTRY_CHOICES 

class SomeForm(forms.Form):
country = forms.ChoiceField(choices = COUNTRY_CHOICES,label = u'Country' )


I'm really new to django and I'm building a form and I need a select box for the user to select their country. I'm playing with the ChoiceField and I created a separate py file with a list of countries. my layout is something like this:

form file:

from django import COUNTRIES #I have a py file with the list of countries

country = forms.ChoiceField(COUNTRIES, label=u'Country')

I kinda didn't expect it to work - I got an error saying that it cannot import name countries . I don't know what step to take to achieve my goal. Any helpful tips?

解决方案

you can import modules the same way you would in a non django python module.

The module you made with the list of countries is not a django module, so you don't import it from django.

If you had a file, my_choices.py which looked like this:

extras.py

COUNTRY_CHOICES = ( ('USA', 'USA'),
                    ('JPN', 'JAPAN'),
                    ('CAN', 'CANADA') )

and it is located in your project's root dir:

myproject/
|_ __init__.py
|_ my_choices.py
|_ settings.py
...

if your project is in the PYTHONPATH, you can import the choices by typing:

>>> from my_choices import COUNTRY_CHOICES
>>> print COUNTRY_CHOICES
(('USA', 'USA'), ('JPN', 'JAPAN'), ('CAN', 'CANADA'))
>>> 

i usually put stuff like this in a utils folder:

myproject/
|_ __init__.py
|_ utils/
   |_ __init__.py
   |_ my_choices.py

so:

>>> from utils.my_choices import COUNTRY_CHOICES
>>> COUNTRY_CHOICES
(('USA', 'USA'), ('JPN', 'JAPAN'), ('CAN', 'CANADA'))
>>> 

in your forms.py

from utils.my_choices import COUNTRY_CHOICES

class SomeForm(forms.Form):
    country = forms.ChoiceField(choices=COUNTRY_CHOICES, label=u'Country')

这篇关于如何在Django中创建一个包含表单的国家列表的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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