python - Don't reload the view Django -
i'm doing set of questions, every question answered yes or no. depends of answer other different question , process repeat several times. problem every time user sends answer view reloaded , show first question. i'm new in django , web programming. answers.
class expertoview(formview): template_name = "sisexperto.html" form_class = formexpert success_url = 'sisexperto' def __init__(self): self.arbol = eval(self.filetostr('sisexperto/enfermedades.txt')) #evalua el archivo self.nodoactual = self.arbol self.pregunta = self.nodoactual[0] def form_valid(self, form): #this method calle when valid data has been posted #it should return httpresponse if len(self.nodoactual) == 3: [self.pregunta, yesnode, nonode] = self.nodoactual if form.is_valid(): form_data = form.cleaned_data respuesta = form_data.get('campo') if respuesta == 'y': self.nodoactual = yesnode else: self.nodoactual = nonode self.pregunta = self.nodoactual[0] #el siguiente return es obligatorio return super(expertoview, self).form_valid(form) def get_context_data(self, **kargs): """ use add context """ context = super(expertoview, self).get_context_data(**kargs) context["pregunta"] = self.pregunta return context
you should bit more on how framework , http works.
first, http stateless, doesn't remember state of last request. "hack" remember did last summer, meant request, use sessions.
secondly, every http request django going instantiate new expertoview
. if update self.nodoactual attribute, next request (which doesn't know nothing last request) going start self.arbol
. here should use session in view.
# maybe this? if form.is_valid(): form_data = form.cleaned_data respuesta = form_data.get('campo') if respuesta == 'y': self.requests.session['nodoactual'] = yesnode else: self.requests.session['nodoactual'] = nonode self.pregunta = self.nodoactual[0]
godspeed.
Comments
Post a Comment