【问题标题】:How to make a Django contact me form with send_mail?如何使用 send_mail 制作 Django 与我联系表格?
【发布时间】:2019-07-14 17:13:05
【问题描述】:

我尝试使用 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 人。谢谢。

【问题讨论】:

    标签: python django email templates


    【解决方案1】:

    您需要 CSRF 令牌:

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

    this official documentation example

    【讨论】:

    • 我只是在表格下面拍它吗?我这样做了,到目前为止没有任何改变
    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 2015-11-12
    • 2016-09-11
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    相关资源
    最近更新 更多