2๐
โ
If the form is invalid, you have to add data['form'] = form
to redisplay the form on the page. Or better, do not use the data
variable at all:
def contact(request):
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, email, ['inbox01@yahoo.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('/contact/thanks/')
else:
form = ContactForm(
initial={'subject': 'I love your site!'}
)
return render(request, 'contact_form.html', {'form': form})
๐คUdi
Source:stackexchange.com