python - 无法在 Django 1.9 中发送电子邮件

标签 python django forms email post

我已经使用 Django 制作了一个联系表单,但我无法使用我的 send_email 方法。

我的表单模板如下所示:

        <form action="/contact/auth" method="post">
            {% csrf_token %}
            <br><br>
            <input type=text class="formfields" placeholder="Name" name="name">
            <br><br>
            <input type=email class="formfields" placeholder="Email" name="contact_email">
            <br><br>
            <textarea type=text placeholder="Tell Us About Your Project" name="message"></textarea>
            <br><br>
            <input type="hidden" value="" id="num1setup" name="num1setup">
            <input type="hidden" value="" id="num2setup" name="num2setup">
            <div id="sendcontainer">
                <div id="whatis">What is &nbsp</div><div id="num1"></div><div id="plus">&nbsp + &nbsp</div><div id="num2">?</div><br>
                <div id="sumcontainer">
                    <input id="sum" type="sum" name="sum">
                    <br><br>
                </div>
                <input class="submit" type=submit value="Send">
            </div>
        </form>

(我意识到我可能应该使用 Django 表单类来执行此操作,但我正在尝试使用原始 HTML 表单来执行此操作。)

从我的 urls.py 文件中调用 /contact/auth:

url(r'^contact/', views.ContactView.as_view(), name='contact'),
url(r'^contact/auth/', views.send_email),

它调用我的 views.py 文件中的 send_email 方法:

from django.views import generic
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect

class ContactView(generic.TemplateView):
    template_name = 'contactpage.html'
    print("contactpage")

def send_email(request):
    print("working")
    name = request.POST.get('name')
    contact_email = request.POST.get('contact_email')
    subject = 'Message from: %s' %name
    message = request.POST.get('message')

    if subject and message and contact_email:
        try:
            send_mail(subject, message, contact_email, ['example@example'], fail_silently=False)
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        return HttpResponseRedirect('/contact/thanks')
    else:
        # In reality we'd use a form class
        # to get proper validation errors.
        return HttpResponse('Make sure all fields are entered and valid.')

这成功重定向到 /contact/auth 但此后没有任何反应。我已尝试测试 send_email 方法,但它似乎没有被调用。

我还在终端中得到以下输出:

最佳答案

作为 Django doc提到过,

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

/contact/auth/ 的调用首先与 /contact 匹配,并且您没有在 中提供 post 函数>ContactView,这就是你得到 405 的原因。因此有两种方法可以解决此问题:

1

只需将 /contact/auth/ 更改为 /contact123/auth/(注意尾部斜杠)。

我建议你这样写:

url(r'^contact123/auth/', views.send_email, name='post_url'),

还有这个:

<form action="{% url 'post_url' %}" method="post">
...

这将使您的网址模式的更改变得更加容易。

2.

添加尾随 $,如下所示 url(r'^contact/$',views.ContactView.as_view(), name='contact'),

根据我的测试,这两种方法都工作得很好。

关于python - 无法在 Django 1.9 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34056169/

相关文章:

python - pandas resample interpolate 正在生成 NaN

python - 如何在Python中用随机森林预测多个类?

python - 从docker中的另一个容器连接到redis

python - Django URL 正则表达式 "is not a valid regular expression"错误

ajax - MVC5、AjaxHelper 和正确的脚本和加载顺序

javascript - 由于表单未连接而取消表单提交

jquery - 使用 jQuery 更改不同域上 iframe 的文本框值?

python - python 中的名称匹配

javascript - 在 Django View 中将 markdown 从文件传递到模板

javascript - 在 Django 项目中使用 typeahead.js