Python Django RegexValidator不显示消息

Python Django RegexValidator不显示消息,python,django,Python,Django,为什么RegexValidator不显示该消息?看起来我发布的代码太多了。。。 以下是我的视图.py: from polls.forms import ContactForm from django.template.loader import get_template from django.core.mail import EmailMessage from django.template import Context, Template, RequestContext from djang

为什么RegexValidator不显示该消息?看起来我发布的代码太多了。。。 以下是我的视图.py:

from polls.forms import ContactForm
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context, Template, RequestContext
from django.shortcuts import render
from django.shortcuts import redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string, get_template

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

 def contact(request):
    form_class = ContactForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')
            phone_number = request.POST.get('phone_number', '')
            content = request.POST.get('content', '')

            # Email the profile with the 
            # contact information

            message = render_to_string('contact_template.txt', {'contact_name': contact_name, 'contact_email': contact_email, 'phone_number': phone_number, 'form_content': content}, context_instance=RequestContext(request))


            email = EmailMessage("New contact form submission", message, "annadrybulska@gmail.com" +'', ['annadrybulska@gmail.com'], headers = {'Reply-To': contact_email })
            email.send()

            return redirect('contact')

    return render(request, 'contact.html', {'form': form_class,})
这是我的forms.py(RegexValidator给我带来麻烦的地方!):

和my template.py:

{% block content %}
<h1>Formularz kontaktowy</h1>
<form role="form" action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Wyślij</button>
</form>
{% endblock %}
{%block content%}
康塔克托维公式
{%csrf_令牌%}
{{form.as_p}}
威利
{%endblock%}

如果您的post请求是
post
且表单无效,则您将传递到模板上下文
form\u class
而不是
form
。要解决此问题,请将最后一行视图更改为这三行:

    else:
        form = form_class()

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

如果没有post数据,它将创建空表单,并将
form
而不是
form\u class
传递到模板。

是否显示任何验证错误,或者仅此错误?您应该显示更多的代码:表单的其余部分、视图和模板。@Daniel Roseman,电子邮件一个正在工作,但它是一个内置的,我将立即添加更多代码。。谢谢:)
    else:
        form = form_class()

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