1

i'm working on Forms in Django, i have a choice field which contain a list of continents and i need to work on the selected option this is the Select request from my 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

and this is my form

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())

so the question is how can i use the selected option in my view

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)

any idea ???

4
  • 2
    You can print form.cleaned_data and see the data yourself. You can then figure out how to access it. BTW, may I know why aren't you using the Django's ORM and using raw MySQLdb cursor? Mar 27, 2013 at 10:59
  • What's wrong with your commented-out code? What happens when you try it? Mar 27, 2013 at 14:36
  • @bibhas i'm new in django have you a tutorial about Django's ORM ?
    – Imoum
    Mar 27, 2013 at 15:16
  • @AmineAntri When learning something new, the first thing you go through is the documentation. Head for docs.djangoproject.com/en/1.5. Select the right version of Django. Mar 27, 2013 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.