0

I have a problem that I have been struggling with for a few days.

I need to assign users to one of three different surveys at random. Each survey is contained in a SessionWizardView. I have been trying to figure out how to assign the users when they click a start button which I have wrapped in a <form>

start.html

<form action="" method="get">
<a class="btn btn-success">START</a>
</form>

urls.py

My URLconf for the start page and for each of the SessionWizardViews

url(r'^start/$', survey_views.start),

url(r'^surveyone/$', SurveyWizardOne.as_view([SurveyFormA, SurveyFormB, SurveyFormC, SurveyFormD ])),
url(r'^surveytwo/$', SurveyWizardTwo.as_view([SurveyFormA, SurveyFormB, SurveyFormC, SurveyFormD ])),
url(r'^surveythree/$', SurveyWizardThree.as_view([SurveyFormA, SurveyFormB, SurveyFormC, SurveyFormD ])),

views.py

def start(request):
    return render(request, 'start.html')

class SurveyWizardOne(SessionWizardView):
    def done(self, form_list, **kwargs):       
        return render_to_response('Return_to_AMT.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

class SurveyWizardTwo(SessionWizardView):
    def done(self, form_list, **kwargs):          
        return render_to_response('Return_to_AMT.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

class SurveyWizardThree(SessionWizardView):
 def done(self, form_list, **kwargs):       
     return render_to_response('Return_to_AMT.html', {
         'form_data': [form.cleaned_data for form in form_list],
     }) 

Im not really sure where to progress from here. I am totally confused by how to get the URL back to the user, I hope that makes sense. Im quite new to Django/Python and have never used an MCV framework before.

I would be able to figure out the random aspect of it as I have done stuff like this years ago in Java, but it was all in one self contained program. In this case I'm not even sure where it should go

1
  • Choose one at random and then return a 302 redirect to that view you chose with url for. Aug 22, 2014 at 16:27

1 Answer 1

4

You can either generate the random link in view code, or in template using built in django random template filter (https://docs.djangoproject.com/en/dev/ref/templates/builtins/#random). Code below is for the first choice:

def start(request):
    survey_urls = ['/surveyone/', '/surveytwo/', '/surveythree/']
    survey_url = random.choice(surveys)
    return render(request, 'start.html', {'survey_url': survey_url})

And then in your template do the standard

<a class="btn btn-success" href="{{survey_url}}">START</a>

You can also consider giving your urls name parameter in urls.py and using reverse function(https://docs.djangoproject.com/en/dev/ref/urlresolvers/#django.core.urlresolvers.reverse) so survey_urls list is not hardcoded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.