Answer the question
In order to leave comments, you need to log in
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')
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