如何从Django Form中的ChoiceField中获取所选选项?

时间:2013-03-27 10:22:53

标签: django forms choicefield

我正在使用Django中的Forms,我有一个包含大陆列表的选择字段,我需要处理选定的选项 这是我的数据库中的选择请求:

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)

任何想法???

0 个答案:

没有答案