繁体   English   中英

send_mail() 获得了参数“fail_silently”的多个值 Django 联系表单仅传递消息而不是名称或主题

[英]send_mail() got multiple values for argument 'fail_silently' Django contact form only passing message not name or subject

所以我正在尝试使用 django 制作联系表格,但它只适用于消息,我试图为发件人姓名及其 email 设置一个区域。

这是我的views.py中的工作代码

from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings



def index(request):

   if request.method == 'POST':
       message = request.POST('message')
       send_mail('Contact Form',
        message,
        settings.EMAIL_HOST_USER,
        ['myemail@gmail.com'], 
        fail_silently=False)
   return render(request, 'app/index.html')

和 html

<h3>Contact Form</h3>

<form method="post" action="{% url 'index' %}">
    {% csrf_token %}
    <input type="text" name="name" placeholder="Your Full Name." />
    
    <textarea name="message" placeholder="Message..."></textarea>
    <input type="submit" />
</form>

如果我尝试设置

name = request.POST.get('name')

在 message = request.POST.get('message') 下方,然后在 send_mail function 中的消息后命名我收到错误send_mail() got multiple values for argument 'fail_silently'我查看了文档,我已经看到几种不同的东西,但没有任何东西能按我需要的方式工作。

更好的方法是创建 HTML 页面用于邮件格式化。 并传递该 HTML 页面中的所有数据。 此 HTML 格式可用作 send_mail 'html_message' 字段中的消息。 请检查以下代码段是否相同:

视图.py:

def index(request):

   if request.method == 'POST':
       message = request.POST.get('message')
       name = request.POST.get('name')
       ctx = {
           'name' : name,
           'message' : message
       }
       message = render_to_string('mail.html', ctx)
       send_mail('Contact Form',
        message,
        settings.EMAIL_HOST_USER,
        ['myemail@gmail.com'], 
        fail_silently=False, html_message=message)
   return render(request, 'app/index.html')

邮件.html:

<body>
  <h1>This is HTML page for mail</h1>
  <p>{{name}}</p>
  <p>{{message}}</p>
</body>

暂无
暂无

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

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