Passing UUID from one view to another

Hi,

I have 5 views:

  • AccueilView and EntreeView;
  • AdherentView, AncienView and SympathisantView.

I want to create a user_identifier on AccueilView, that will be saved in the database (Consentement model). Then users reach the EntreeView, which allow them to chose in which category they fit. When they have chosen it, they’re sent to the corresponding view among the three other (AdherentView, AncienView and SympathisantView).

I want to create a UUID (user_identifier) at the first step, and pass it through the other views to one of the three views. So far, I’ve suceeded in creating and storing the user_identifer inside AccueilView, but I’m not sure how to proceed after that. I’ve written the following views but it stops working at EntreeView: whatever I chose I keep being sent back to accueil.

Any suggestions would be very welcome.

views.py:

class AccueilView(FormView):
    model = Consentement
    form_class = AccueilForm
    template_name = 'form1/accueil.html'
    success_url = '/entree/'

    def form_valid(self, form):
        qcon01_consent = form.cleaned_data['qcon01_consent']
        user_identifier = str(uuid.uuid4())
        response = HttpResponse()
        response.set_cookie('user_identifier', user_identifier)
        consent = Consentement(user_identifier=user_identifier, qcon01_consent=qcon01_consent)
        consent.save()
        return super().form_valid(form)


class EntreeView(FormView):
    model = Categorie
    form_class = EntreeForm
    template_name = 'form1/entree.html'
    success_url = 'form1'

    def form_valid(self, form):
        answer = form.cleaned_data['categorie']
        user_identifier = self.request.COOKIES.get('user_identifier', None)
        if user_identifier is None:
            return redirect('accueil')
        self.request.session[user_identifier] = answer
        if answer == 'sympathisant':
            return redirect('sympathisant')
        elif answer == 'ancien':
            return redirect('ancien')
        else:
            return redirect('adherent')


def adherent_view(request):

    if request.method == 'POST':
        form = AdherentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/conclusion/')
    else:
        form = AdherentForm()

    context = {'form': form}
    return render(request, 'form1/adherent.html', context)


def ancien_view(request):

    if request.method == 'POST':
        form = AncienForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/conclusion/')
    else:
        form = AncienForm()

    context = {'form': form}
    return render(request, 'form1/ancien.html', context)


def sympathisant_view(request):

    if request.method == 'POST':
        form = SympathisantForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/conclusion/')
    else:
        form = SympathisantForm()

    context = {'form':form}
    return render(request, 'form1/sympathisant.html',context)


def conclusion_view(request):

    if request.method == 'POST':
        form = ConclusionForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/merci/')
    else:
        form = ConclusionForm()

    context = {'form': form}
    return render(request, 'form1/conclusion.html', context)


class MerciView(TemplateView):
    template_name = 'form1/merci.html'

There are a couple ways of doing this.

  • You could pass the uuid as a url parameter from view to view. It’s actually the cleanest, but can’t reliably handle any sort of “interruption” in the flow.

  • You could store the uuid in the user’s session. That allows for more flexibility when moving from url to url, but that information only lasts as long as the session lasts.

  • You could store the uuid in the database. This is only really practical with authenticated users, but at least allows for using this sequence of events across sessions.

Your decision is, in part, going to depend upon whether someone using this is going to complete it in one sitting, whether the users are authenticated, and whether they’re required to follow this through directly or if they can stop mid-way and come back to it later.

1 Like

This is probably the way I’d like to do it. It’s related to this other thread where we discussed the possibility for the respondent to come back to the survey and finish to answer it later:

Using this UUID field could be a solution to this too - or even better, instead of a UUID, as you mentioned in the other thread, I could harvest a string of character like a “name” from the respondent and store it in the database, to use it the same way that I would use the UUID: a unique identifier that could be retrieved or entered by the respondent to repopulate the form, even on another session.

Let’s stick on the UUID for this thread. How should I proceed to pass the value of the UUID from one view to another? Or to put it another way, how to make sure that even if the view changes, the user is still filling the same object data in the database?

I know you’re not intending to just repeat the original question, so I guess I’m not following you here. You can pass the UUID using any of the three methods I outlined above - using the URL, session, or the database.

Sorry, I had to go back and re-read the other thread. I had forgotten much of what we had talked about.

If you’re using the database with some not-user-type information, then for each page, the user would need to enter their “unique identifier” to retrieve the information for that page. I’m sure that’s not what you want.

I’d suggest you have a “home” page, where someone would enter in their name. That page can then retrieve the uuid, and check the database to see what stage they’re at when filling out the form.

You would use that information to construct a url to be used as the “success url” for the view handling the form.

You would include the uuid as a url parameter for each view - with code checking for the presence of the uuid as a parameter. If it’s not supplied, you may want to return them to the home page. If it is supplied, but it’s not an active form, you might want to display a page telling them they need to start over - or whatever is appropriate. That flow logic would be up to you.

1 Like