S
S
Stanislav Shabalin2020-09-04 21:37:06
Django
Stanislav Shabalin, 2020-09-04 21:37:06

Why doesn't redirect work after form validation?

I can't figure out why, after sending a message, I stay on the main page of the form and there is no redirect?

#views.py
def index(request):
  if request.method == "POST":
    form = ContactForm(request.POST)

    if form.is_valid():
      cd = form.cleaned_data
      send_email(request)
  else:
    form = ContactForm()

  return render(request, 'index.html', {'form': form})


def send_email(request):

  template = render_to_string('message_for_recepient.html', {
    'name':request.POST['name'],
    'email':request.POST['email'],
    'message':request.POST['message'],
  })

  email = EmailMessage(
    'Новое сообщение с сайта!',
    template,
    settings.EMAIL_HOST_USER,
    settings.EMAIL_RICIPIENTS,

  )
  email.content_subtype = "html"
  email.fail_silently=True
  try:
   	email.send()
  except BadHeaderError: 
   	return HttpResponse('Неверный заголовок письма')

  return render(request, 'thanks_for_message.html')


Everything seems to be simple, but does not want to work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-09-04
@Starck43

if form.is_valid():
      cd = form.cleaned_data
      return send_email(request)

returnmissed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question