django - How to store incremental counter variables in a `class Counter(models.Model):` and therefore the Database? -


i building/built small survey application meant direct users 1 of 9 surveys. appending url name of survey e.g. /surveyone/, /surveytwo/ etc.

each path/survey has maximum amount of times should seen, e.g. pathmax = 3 , total number of competed surveys (all 9 paths) cannot exceed, 20 e.g. totalmax = 20

when survey complete counter incremented in done() method e.g. surveywizardonecounter += 1 , checked against pathmax when assigning participant path.

the problem:

the code below works fine on local machine, on server counter variables totalmaxcounter , e.g. surveywizardonecounter never increment past 1. believe because getting reset 0 @ top of file.

can tell me how past this? can/should store counter variables can update them , access them without them getting overwritten?

suspicions:

i believe should storing these counter variables in model/database e.g. class counter(models.model): not sure how or how access them.

i have never used/learned database before , have managed set 1 (mysql) store person model data survey application.

views.py

pathmax = 3  totalmax = 20 totalmaxcounter = 0  surveywizardonecounter = 0 surveywizardtwocounter = 0  surveywizardthreecounter = 0  surveywizardfourcounter = 0  surveywizardfivecounter = 0  surveywizardsixcounter = 0  surveywizardsevencounter = 0  surveywizardeightcounter = 0  surveywizardninecounter = 0    def begin(request):     global totalmaxcounter         if totalmaxcounter < totalmax:              survey_url = random.choice(survey_urls)         print 'you looking at:', survey_url                  if survey_url == '/surveyone/':                  if surveywizardonecounter > pathmax:                 survey_urls.pop(0)          elif survey_url == '/surveytwo/':             if surveywizardtwocounter > pathmax:                 survey_urls.pop(1)          elif survey_url == '/surveythree/':             if surveywizardthreecounter > pathmax:                 survey_urls.pop(2)          elif survey_url == '/surveyfour/':             if surveywizardfourcounter > pathmax:                 survey_urls.pop(3)          elif survey_url == '/surveyfive/':             if surveywizardfivecounter > pathmax:                 survey_urls.pop(4)          elif survey_url == '/surveysix/':             if surveywizardsixcounter > pathmax:                 survey_urls.pop(5)          elif survey_url == '/surveyseven/':             if surveywizardsevencounter > pathmax:                 survey_urls.pop(6)                          elif survey_url == '/surveyeight/':             if surveywizardeightcounter > pathmax:                 survey_urls.pop(7)             elif survey_url == '/surveynine/':             if surveywizardninecounter > pathmax:                 survey_urls.pop(8)            return render(request, 'begin.html', {'survey_url': survey_url})         else:         return render(request, 'surveyfull.html')       class surveywizardone(sessionwizardview):                                  def get_context_data(self, form, **kwargs):         context = super(surveywizardone, self).get_context_data(form, **kwargs)                               ...         ....          return context        def done(self, form_list, **kwargs):         global surveywizardonecounter         global totalmaxcounter          surveywizardonecounter += 1            totalmaxcounter += 1          logger.debug('\n\n surveywizardonecounter = %s', surveywizardonecounter)         logger.debug('\n\n totalmaxcounter = %s', totalmaxcounter)          form in form_list:             form.save()          return render(self.request, 'return_to_amt.html', {             'form_data': [form.cleaned_data form in form_list],                     })   

do not save in global variable in web app. requests handled in different sessions, possibly different python interpreter instances, global variable set in 1 session not visible in session.

counters can saved in database, field in survey model, , total count can calculated sum on field.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -