Django 联系我们部分

Django contact us section

我是 Web 开发的新手,我有一页 django 组合应用程序,其中有用于联系我们的部分,我使用了基于 class 的视图和 post 方法。但是,当我单击提交按钮时,我认为 POST 请求未被调用,因此 post 函数中的 send_mail 函数无法正常工作。

views.py


class HomeView(TemplateView):
    template_name = 'home.html'
    http_method_names = ['post', 'get']

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['about'] = About.objects.first()
    context['services'] = Service.objects.all()
    context['works'] = RecentWork.objects.all()

    return context
@csrf_exempt
def post(self, request):
    if request.method == "POST":
        message = request.POST['message']
        try:
            send_mail('Test Subject', message, settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER])
        except Exception:
            print(Exception)
            return HttpResponse('Error: Invalid header found')
    return HttpResponse('success')

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.HomeView.as_view(), name='home'),
]

home.html

<div class="invitation-area section-padding">
        <div class="container">
            <div class="row">
                <div class="single-invite text-center">
                    <h2>Do you have any project need to be done?</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias at cupiditate distinctio dolorem
                        dolores impedit, inventore magnam magni nam numquam quidem ratione rem repellendus, saepe?</p>
                    <a href="#contact" class="my-btn-3">Get started</a>
                </div>
            </div>
        </div>
    </div>

    <div id="contact" class="contact-area section-padding">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="section-header text-center">
                        <h2>Contact Me</h2>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2 box-contact-form">
                    <form id="contact-form" method="post">
                        {% csrf_token %}

                        <div class="messages"></div>

                        <div class="controls">

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group wow fadeInDown" data-wow-delay="0.2s">
                                        <input id="form_name" type="text" name="name" class="form-control"
                                               placeholder="Enter your full name *" required="required"
                                               data-error="Fullname is required.">
                                        <div class="help-block with-errors"></div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group wow fadeInDown" data-wow-delay="0.4s">
                                        <input id="form_email" type="email" name="email" class="form-control"
                                               placeholder="Enter your email *" required="required"
                                               data-error="Valid email is required.">
                                        <div class="help-block with-errors"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group wow fadeInUp" data-wow-delay="0.6s">
                                        <textarea id="form_message" name="message" class="form-control"
                                                  placeholder="Your Message *" rows="4" required="required"
                                                  data-error="Leave a message for me"></textarea>
                                        <div class="help-block with-errors"></div>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <button class="btn btn-send" value="submit"><a href={% url 'home' %}> Send message</a></button>
                                </div>
                            </div>
                        </div>

                    </form>

                </div>

            </div>

首先,根据我的评论,您的按钮不是提交按钮。其次,出于某种原因,您的按钮内有一个锚标记。单击锚标记显然会发出 GET 请求。 更改您的按钮:

<button class="btn btn-send" value="submit"><a href={% url 'home' %}> Send message</a></button>

收件人:

<button type="submit" class="btn btn-send" value="submit">Send message</button>