How to add URL strings for a Django app to a database and randomly select? -


i built small survey application randomly assigns participants 1 of 9 paths randomly selecting last part of url , appending onto button link. use e.g. {% if 'surveyone' in request.path %} etc. in template tags display right content user.

this works fine on local machine have since discovered multi-threaded processing , using constant lists such below survey_urls has caused issues when project uploaded server. want instead store in database.

question(s)

  • how add/store below survey_urls list of strings in mysql database?
  • how remove particular url strings same database? (as below)

the below existing code has functionality need , works fine on machine. need make work correctly when dealing multiple concurrent users.

thanks

views.py

survey_urls = ['/surveyone/', '/surveytwo/', '/surveythree/', '/surveyfour/', '/surveyfive/', '/surveysix/', '/surveyseven/', '/surveyeight/', '/surveynine/']  def begin(request):      total_path_counter = totalcounter.objects.get_or_create(total_max_counter__gte=0)[0]       if total_path_counter.total_max_counter < 100:          survey_url = random.choice(survey_urls)          if survey_url == '/surveyone/':                 survey_path_counter = surveycounter.objects.get_or_create(survey_wizard_type = 'survey_wizard_one')[0]              if survey_path_counter.survey_wizard_count > 9:                  survey_urls.remove('/surveyone/')          elif survey_url == '/surveytwo/':                 survey_path_counter = surveycounter.objects.get_or_create(survey_wizard_type = 'survey_wizard_two')[0]              if survey_path_counter.survey_wizard_count > 9:                  survey_urls.remove('/surveytwo/')          ....         ....           elif survey_url == '/surveynine/':             survey_path_counter = surveycounter.objects.get_or_create(survey_wizard_type = 'survey_wizard_nine')[0]              if survey_path_counter.survey_wizard_count > 9:                 survey_urls.remove('/surveynine/')          return render(request, 'begin.html', {'survey_url': survey_url})         else:         return render(request, 'surveyfull.html') 

begin.html

<a class="btn btn-success" href="/experiment{{survey_url}}">begin instruction tasks</a> 

well, suppose can make new model this:

class survey(model.model):   name = models.charfield(max_length=50) # surveyone goes here   ... 

than in view can pull random this:

survey_count = survey.objects.all().count() random_survey_index = random.randint(0, survey_count - 1) 

get instance name random_survey_index , append url

not sure how effective approach is, should work.


Comments