python - Multiple Django functions on class-based view -


i'm looking using multiple django functions in same class , display these functions on same template.

both functions in django template, none work , don't know why.

first function :

this function lets search person in database based on 3 criteria : firstname, lastname , birthcity. display query result in array.

second function :

this function simple django form.

this first django class :

class societeformulaire(templateview) :      template_name= "societe.html"     model = societe       def get_context_data(self, **kwargs):          request = self.request          if 'recherche' in request.get:              query_lastname_id = request.get.get('q1lastnameid')             query_firstname_id = request.get.get('q1firstnameid')             query_birthcity_id = request.get.get('q1birthcityid')              sort_params = {}              set_if_not_none(sort_params, 'lastname__icontains', query_lastname_id)             set_if_not_none(sort_params, 'firstname__icontains', query_firstname_id)             set_if_not_none(sort_params, 'birthcity__icontains', query_birthcity_id)              query_list = societe_recherche.recherche_filter(societe, sort_params)              context = {                 "query_lastname_id" : query_lastname_id,                 "query_firstname_id" : query_firstname_id,                 "query_birthcityid" : query_birthcity_id,                 "query_list" : query_list,             }              return context       def get_context_data(self, **kwarg) :          success = false         request = self.request          if request.method == 'post':              form = individuformulaire(request.post or none, request.files or none)              if form.is_valid() :                 post = form.save(commit=false)                 element in settings.bdd :                     post.save(using=element, force_insert=true)                  messages.success(request, 'le formulaire été enregistré !')                 return httpresponseredirect(reverse('individuresume', kwargs={'id': post.id}))              else:                 messages.error(request, "le formulaire est invalide !")          else:             form = individuformulaire()             form.fields['utilisateur'].initial = request.user.last_name + " " + request.user.first_name          context = {             "form" : form,             "individu" : individu         }          return context 

i named both functions same name, because if don't that, first function or form not displayed on template.

so question : how can set multiple functions in same django class , in same django template ?

you have several misconceptions , misunderstandings here.

you can't have 2 methods same name in python class. first 1 overridden when class loaded. normal solution put code in 1 method, or have 2 different names , call 1 other.

however, should consider none of logic belongs in get_context_data anyway. that's preparing context data template, not processing form submissions.

the fundamental problem picking wrong class inherit , overriding wrong methods. templateview basic class , not meant used this. real action of view display , process form creates element; so, should use createview. can rid of code in second method; view processing you, thing need define form_valid custom save logic.

the search logic remain in get_context_data if like. need remember call superclass method, , return context if 'recherche' in request.get false.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -