简体   繁体   English

Python - Django 当我提交表单时提交按钮不起作用

[英]Python - Django The submit button does not work when I submit a form

I'am developing a website in Python Django.我正在 Python Django 开发一个网站。 I want to code a form which send me by email the email address that the users put in the form.我想编写一个表格,通过 email 向我发送用户在表格中输入的 email 地址。 The problem is that the submit button does not work.问题是提交按钮不起作用。 When I click the button submit, I am not redirected to a new page and I am not receiving the content of the form.当我单击提交按钮时,我没有被重定向到新页面,也没有收到表单的内容。 Can you help me to fix it?你能帮我修一下吗?

My views我的看法

def test(request): 
    if request.method == 'GET':
        form = newsletterForm()
    else:
        form = newsletterForm(request.POST)
        if  form.is_valid():
            subject = 'New Subscriber'
            from_email = 'no-reply@test.com'
            message =  ' Email Adress: ' + form.cleaned_data['from_email']

            try:
                send_mail(subject, message, from_email, ['info@test.com'])
                messages.success(request, 'Thank you for contact us we will get back soon')
                render(request, "Test.html")

            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            render(request, "Test.html")

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

My HTML我的 HTML

<form id="newsletterForm" class="contact-form" action="#" method="post">
    <div class="input-group input-group-rounded">
         {{ form.from_email|add_class:"form-control" }}
        <span class="input-group-append">
        <button class="btn btn-light text-color-dark" type="submit"><strong>GO!</strong></button>
        </span>
    </div>
</form>

My forms我的 forms

class newsletterForm(forms.Form):
    from_email = forms.EmailField(label='search',
                    widget=forms.TextInput(attrs={'placeholder': 'Email Address'}))

Settings设置

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'no-reply@test.com'  
EMAIL_HOST_PASSWORD = ‘xxx’
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'default from email'

Set the form action to view url eg设置表单动作查看 url 例如

 <form id="newsletterForm" class="contact-form" action="{%url ‘view_name’%}" method="post">

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

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