user2170928
user2170928

Reputation: 247

passing variables in django forms

I'm writing my first django app and I'm in trouble. This is my sitiuation:

I split a track in diferent ways and I need to show every one in an html page with a form. The user must fill the form about this part of the track and then I need to show the next way with the form again until it finishes. I don't know how to do that loop, but I've tried with this and my problem is that I don't know how to pass de variables that I need from the GET situation to the POST.

def acoplar_track(request, track_id, ):            
    if request.method=='POST':
        formulari = WayForm(request.POST, request.FILES)
        if formulari.is_valid():   
            x.addFeatures(newWays_l[cont], formulari.descripcio, formulari.tipus)    
            x.saveCityToFile('/var/www/tottrack/media/zones/city/'+track.zona.nom)                  

            if cont+1==len(newWays_l):
                return render_to_response('principal/inici.html', context_instance=RequestContext(request))
            else:
                cont = cont+1          
                formulari = WayForm()
                lon, lat, zoom = x.getWayMapSettings(newWays_l[cont]) #variables to show the map

                return render(request,'principal/wayForm.html',
                {'formulari':formulari, 'lat':lat, 'lon':lon, 'zoom':zoom})


    else:
        track = get_object_or_404(Track, id=track_id) # obte el track i la zona corresponent al track   

        x=Xarxa('/var/www/tottrack/media/zones/city/'+track.zona.nom) # crea una Track() mitjansant el fitxer on s'ha guardat la zona per poder acoplar-hi les noves dades
        nomTrack = track.track.name.split('/')[-1:] # aconsegueix nomes el nom del fitxer, sense /tracks/gpx/
        x.setZoneBB(track.zona.latSud, track.zona.lonOest, track.zona.latNord, track.zona.lonEst)
        x.uploadTrack(nomTrack[0]) # fusiona el track a la city
        newWays_l = x.getTrackWays() # obte les ids dels nous camins que s'afegiran

        if not newWays_l:
            return render_to_response('acoplarTrackRepetit.html')
        else: 
            cont = 0           
            formulari = WayForm()
            lon, lat, zoom = x.getWayMapSettings(newWays_l[0]) #variables to show the map

            return render(request,'principal/wayForm.html',
            {'formulari':formulari, 'lat':lat, 'lon':lon, 'zoom':zoom})

forms.py

CHOICES = (('1','Pista',),('2','Corriol',))             
class WayForm(forms.Form):
    descripcio = forms.Textarea()
    tipus = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)

Thanks for your help!

Upvotes: 0

Views: 313

Answers (2)

Brenda J. Butler
Brenda J. Butler

Reputation: 1485

Let me rephrase your question, to make sure I understand:

You want to collect some information over several pages/forms, before saving a model.

This is covered in the documentation here:

https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/form-wizard/

Basically, the information collected in earlier pages is actually saved in the database but in a different place, until you have all the info needed to save the model in which you're interested. Then you can save the model and delete all the cached responses.

Upvotes: 0

Iradiel90
Iradiel90

Reputation: 41

Just try put the code of the GET method before the if request.method == POST. Something like this:

def acoplar_track(request, track_id, ):

//Code for GET here

if request.method == POST:

    //Code for POST here

//return render GET method

Upvotes: 1

Related Questions