1

私はDjangoでフォームに取り組んでいます。大陸のリストを含む選択フィールドがあり、選択したオプションで作業する必要があります。これは私のDBからの選択リクエストです:

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