繁体   English   中英

如何使用send_mail使Django与我联系表单?

[英]How to make a Django contact me form with send_mail?

我尝试使用6个不同的教程来完成此操作,但是所有教程都给出了不同的变化,所以我对自己的步伐感到非常沮丧...我想我已经接近最后几步了,但是我需要一些帮助。 这是我在Django项目中拥有的代码:

# -- settings.py--
EMAIL_BACKEND = 'django.core.mail.backends.stmp.EmailBackend'
EMAIL_HOST = 'http://smtp.gmail.com/'
EMAIL_HOST_USER = 'a spare Gmail I have'
EMAIL_HOST_PASSWORD = 'the password'
EMAIL_USE_TLS = False
EMAIL_PORT = 465

..

# -- views.py --
# (assumed relevant imports are imported)
class ContactView(FormView):
    template_name = 'CONTACT.html'
    form_class = ContactForm
    success_url = 'Success!'
    context_vars = {
        'example_name_f': 'Adam',
        'example_name_l': 'Smith',
        'example_email': 'smith.a@gmail.com',
        'example_subject': 'Sales proposal',
        'example_text': 'Hi Mark, I have a sales proposal for you!',
    }
    def get(self, request):

        return render(request, 'CONTACT.html', ContactView.context_vars)

    def contact(self, request):
        if request.method == 'POST':
            form = self.form_class(data=request.POST)

            if form.is_valid():
                time = request.POST.get('time', '')
                first_name = request.POST.get('first_name', '')
                last_name = request.POST.get('last_name', '')
                email_address = request.POST.get('email_address', '')
                subject = request.POST.get('subject', '')
                text = request.POST.get('text', '')

                send_mail(subject, text, email_address,
                          ['999@outlook.com'], fail_silently=False)

            return redirect('CONTACT-done.html')  # CONTACT-done is a temporary success screen

        return render(request, 'CONTACT.html', ContactView.context_vars)

HTML的相关部分:

<div class="container-fluid" style="width: 100%; height: 100%">
        <form action="" method="post">

            <label for="first-name">First Name</label>
            <input id="first-name" name="first-name" type="text" placeholder="{{ example_name_f }}">
                <!-- Note: placeholder vs value attributes similar -->
                <!-- id is for HTML, name is for views.py -->
            <label for="last-name">Last Name</label>
            <input id="last-name" name="last-name" type="text" placeholder="{{ example_name_l }}">
                <!-- Its unnecessary to use context vars for placehoder text -->
            <label for="email">Email Address</label>
             <input id="email" name="email" type="email" placeholder="{{ example_email }}" required>

            <label for="subject">Subject</label>
             <input id="subject" name="subject" type="text" placeholder="{{ example_subject }}">

            <label for="text">Message:</label>
             <input id="text" name="text" type="text" placeholder="{{ example_text }}" required>

            <input type="submit" value="Send">
        </form>
    </div>

有些教程推荐使用HTML的{{form.as_p}}方法,但有些只是像我上面那样做基本的HTML样式。 不知道从这里做什么。 点击我的网站上的发送会导致403 CSRF验证失败。 请求中止。 请以尽可能简单的方式进行说明,我不是编程新手,但我也不是CS人士。 谢谢。

您需要CSRF令牌:

<form ...>
{% csrf_token %}
...
</form>

如此官方文档示例所示

暂无
暂无

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

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