Hi,
I have 5 views:
AccueilViewandEntreeView;AdherentView,AncienViewandSympathisantView.
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'
