Reputation: 4155
I am building/built a small survey application which is meant to direct users to one of 9 surveys. It does this my appending the URL with the name of the survey e.g. /surveyone/
, /surveytwo/
etc.
Each path/survey has a maximum amount of times it should be seen, e.g. PathMax = 3
and the total number of competed surveys (all 9 paths) cannot exceed, 20 e.g. TotalMax = 20
When a survey is complete a counter is incremented in the done()
method e.g. SurveyWizardOneCounter += 1
and this is checked against the PathMax
when assigning a participant their path.
The Problem:
The code below works fine on my local machine, however on the server the counter variables TotalMaxCounter
and e.g. SurveyWizardOneCounter
never increment past 1. I believe this is because they are getting reset to 0
at the top of the file.
Can anyone tell me how to get past this? Where can/should I store the counter variables so that I can update them and access them without them getting overwritten?
Suspicions:
I believe I should be storing these counter variables in a model/database e.g. class Counter(models.Model):
but I am not really sure how to do this or how to access them.
I have never really used/learned database before and have just about managed to set one up (MySQL) to store a Person model with the data from my 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 are 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)
for form in form_list:
form.save()
return render(self.request, 'Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})
Upvotes: 0
Views: 85
Reputation: 18513
Do not save anything in a global variable in a web app. Requests are handled in different sessions, very possibly different python interpreter instances, global variable set in one session is not visible in another session.
Counters can be saved in database, as an extra field in the Survey
model, and the total count can be calculated by Sum
on this field.
Upvotes: 1