私はDjangoに取り組んでおり、選択した値を取得する方法を知る必要がありますforms.ChoiceField
フォーム.py
class FilterForm(forms.Form):
continent = forms.ChoiceField(choices=Select_continent())
私は試してみました:views.py
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.append(('i',row[0]))
i+=1
except:
print "Error: unable to fetch data"
db.close()
return continents
def affiche_all(request, currency):
if request.method == 'POST':
form = FilterForm(request.POST)
if form.is_valid() :
Continent = form.cleaned_data['Continent']
Continent = dict(form.fields['Continent'])[Continent]
url = reverse('affiche_all', args=(), kwargs={'continent': Continent})
return HttpResponseRedirect(url)
しかし、Continent
常に最後の値を返し、ChoiceField
必要なのは選択した値です。
助言がありますか?