1

所以我是这个应用程序,当用户填写表格时,我会收到一封包含他们详细信息的电子邮件,以便我可以联系他们。但是,每当提交表单时,它都会向我显示此错误。

在此处输入图像描述

我找不到什么在窃听代码。所有凭据都已到位。我正在为这个项目使用 SendingBlue smtp 服务器。看看我的代码:

视图.py

from django.shortcuts import render, redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect

# Create your views here.

def home(request):
    return render(request, 'index.html')


def register(request):

    if request.method == 'GET':
        return render(request, 'register.html')

    else:
        name = request.POST['full-name']
        email = request.POST['email']
        phone = request.POST['phone']
        nationality = request.POST['nationality']

        try:
            send_mail("Trial Application", name + email + phone + nationality, '*my email*', ['* same email*'])

        except BadHeaderError:
            return HttpResponse('Something Went Wrong')
        
        return redirect(request, 'index.html')
    return render(request, 'register.html')

设置.py

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp-relay.sendinblue.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "*my email*"
EMAIL_HOST_PASSWORD = "password"

注册.html


        <form action="" method="post" class="form">
            {% csrf_token %}
            <fieldset>
                <legend>Please fill in the required details</legend>
                <input type="text" name="full-name" placeholder="Full Name" id="">
                <input type="email" name="email" placeholder="Email" id="">
                <input type="tel" name="phone" placeholder="Phone Number" id="">
                <input type="text" name="nationality" placeholder="Nationality" id="">

                <input type="submit" value="Submit" id="submit">
            </fieldset>
        </form>

如果有人能帮助我根除这个错误,我将不胜感激。提前致谢。

4

1 回答 1

0

您遇到的错误不是因为 send_mail 功能,而是因为 Django 无法找到 URL:'/apply/'。您在表单的 HTML 中提供的操作在 urls.py 中不存在。请确保操作与您的 django 的 URL 模式匹配。

例如:

<!--If this is your form tag:-->
<form method=post>

您需要向其添加操作,该操作应与您的 django urls.py 的 URL 映射匹配,如下所示:

<form method=post action="/register/account">

单击提交后,操作将表单发送到此 URL:http://localhost/register/account

因此,在 urls.py 你应该有一个匹配模式,如:

urlpatterns = [
path('register/account', views.register, name='register')
]
于 2022-02-15T20:02:25.967 回答