简体   繁体   English

Django RegexValidator不显示消息

[英]Django RegexValidator does not display message

Why the RegexValidator is not showing the message? 为什么RegexValidator没有显示该消息? It looks like I'm posting too much code... Here is my views.py: 好像我发布了太多代码...这是我的views.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,})

here is my forms.py(where RegexValidator is causing me troubles!): 这是我的forms.py(RegexValidator在给我带来麻烦!):

    from django import forms
    from django.core.validators import RegexValidator
    from django.core import validators
    from django.core.exceptions import ValidationError

    #new form
    class ContactForm(forms.Form):
        contact_name = forms.CharField(required=True)
        contact_email = forms.EmailField(required=True)
        phone_number = forms.CharField(required=True, max_length=9, validators=[RegexValidator(regex=r'^\d+$', message='Nieprawidłowy numer telefonu.', code='invalid_number', inverse_match=None, flags=0
    )])
        content = forms.CharField(required=True, widget=forms.Textarea)
        def __init__(self, *args, **kwargs):
            super(ContactForm, self).__init__(*args, **kwargs)
            self.fields['contact_name'].label = "Imię:"
            self.fields['contact_email'].label = "E-mail:"
            self.fields['phone_number'].label = "Numer telefonu:"
            self.fields['content'].label = "Wiadomość:"

and my template.py: 和我的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 %}

You're passing into template context form_class instead of form if your post request is POST and form is not valid. 如果您的发布请求是POST并且表单无效,那么您将传递到模板上下文form_class而不是form To solve the issue, change your last line of view to this three lines: 要解决此问题,请将最后一条视图更改为以下三行:

    else:
        form = form_class()

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

It will create empty form if there is no post data and pass form instead of form_class to your template. 如果没有发布数据,它将创建一个空表单,并将form而不是form_class传递给模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM