如何以Django格式从ChoiceField获取所选选项?
我正在Django中处理表单,我有一个包含大陆列表的选项字段,我需要处理所选选项 这是来自my DB的Select请求:如何以Django格式从ChoiceField获取所选选项?,django,forms,choicefield,Django,Forms,Choicefield,我正在Django中处理表单,我有一个包含大陆列表的选项字段,我需要处理所选选项 这是来自my DB的Select请求: def Select_continent(): db = MySQLdb.connect("localhost", "root", "aqw", "PFE_Project") cursor = db.cursor() sql = "SELECT Continent FROM myform_servercomponents" try:
def Select_continent():
db = MySQLdb.connect("localhost", "root", "aqw", "PFE_Project")
cursor = db.cursor()
sql = "SELECT Continent FROM myform_servercomponents"
try:
cursor.execute(sql)
results = cursor.fetchall()
continents = []
i=0
for row in results:
continents[i]=row[0]
i+=1
except:
print "Error: unable to fetch data"
db.close()
return continents
这是我的表格
def continents():
data = Select_continent()
i=0
continents=[]
for key,value in data:
continents.append(('i',value))
i +=1
return continents
class FilterForm(forms.Form):
Continent = forms.ChoiceField(choices=continents())
因此,问题是如何在视图中使用所选选项
def affiche_all(request, currency):
if request.method == 'POST':
form = FilterForm(request.POST)
if form.is_valid() :
Continent = form.cleaned_data['Continent']
# here i need to send the selected choice to another view
# like this :
# continent == 'Europe'
#url = reverse('affiche_all', args=(), kwargs={'continent': continent})
#return HttpResponseRedirect(url)
有什么想法吗?您可以打印
表单。清除数据
并自己查看数据。然后,您可以了解如何访问它。顺便说一句,我可以知道你为什么不使用Django的ORM和原始MySQLdb
cursor吗?你注释掉的代码有什么问题?当你尝试它时会发生什么?@bibhas我是django的新手,你有关于django的ORM的教程吗?@Aminentri当你学习一些新东西时,你首先要看的是文档。前往。选择Django的正确版本。