Answer the question
In order to leave comments, you need to log in
How to make a redirect after submitting a form with passing a variable to the template?
How to make a page redirect after submitting the form to the same page, but with the transfer of a variable that will be responsible for showing a message on the page about successful submission. If you pass context data to the template before submitting, and after submitting the form, change one of the arguments, nothing changes, and in principle I understand why, but still I can’t figure out how to change or transfer data after submitting. I've been trying to do this for several hours now and find an answer, but without success.
views.py
if request.method == 'POST':
form = FeedbackForm(request.POST)
if form.is_valid():
data = {
'full_name': form.cleaned_data['full_name'],
'email': form.cleaned_data['email'],
'phone': form.cleaned_data['phone'],
'message': form.cleaned_data['message'],
}
context['success'] = True
return HttpResponseRedirect(request.path)
else:
form = FeedbackForm()
context = {
'success': False,
'form': form,
}
return render(request, 'index.html', context)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question