繁体   English   中英

send_mail 仅适用于 django 的终端

[英]send_mail only works in terminal in django

我试图让我的网站在按下按钮时发送 email 。 但是,当我在 shell 中使用 send_mail 时,它只发送 email 并且在按下按钮时什么也不做。

我目前在我的 settings.py 中有以下内容

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = '#'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = '#'

# 当然只针对这个问题

在我的 html 文件中,我有

 <form method="post">
  {% csrf_token %}
  <button type="submit" name="place_order" class="btn btn-success">
   Place Order</button>
 </form>

place_order 与我的 views.py 链接,它检查按钮是否被点击

 checkout = request.POST.get('place_order')
        if checkout:
            send_mail('Django test mail', 'this is django test body', 
            '#', ['#'], fail_silently=False,)
            messages.info(request, f'Your order has been placed!') 

名为place_order的发布请求中没有任何值,因为它是一个button ,而不是input ,因此 if 语句为false并且send_mail不会被触发:

 checkout = request.POST.get('place_order')
        if checkout: # <=== here
            send_mail('Django test mail', 'this is django test body', 
            '#', ['#'], fail_silently=False,)
            messages.info(request, f'Your order has been placed!') 

因此,如果您将HTML更改为:

<form method="post">
  {% csrf_token %}
  <input type="hidden" name="place_order" value="blablabla"/> # value shouldn't be empty
  <button type="submit" class="btn btn-success">
   Place Order</button>
 </form>

暂无
暂无

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

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