1

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

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)

任何想法 ???

4

0 回答 0