D
D
dintear_koden2021-10-17 20:26:46
Django
dintear_koden, 2021-10-17 20:26:46

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

1 answer(s)
S
Sergey Gornostaev, 2021-10-17
@dintear_koden

https://docs.djangoproject.com/en/3.2/ref/contrib/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question