Django-提交联系表单时出现405错误

时间:2020-02-15 12:15:54

标签: python django python-3.x http-status-code-405

我的HTML联系人表格

<form id="contact-form" method="post" action="{% url 'index' %}">
                        {% 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" type="submit">Send message</button>
                                </div>
                            </div>
                        </div>

                    </form>

urls.py

from django.urls import path
from .views import HomeTemplateView

urlpatterns = [
    path('', HomeTemplateView.as_view(), name="index")
]

views.py,我使用处理我的联系表的方法索引定义了HomeTemplate视图类

from django.shortcuts import render
from django.views.generic import TemplateView
from .models import *
from django.core.mail import send_mail
from django.conf import settings


class HomeTemplateView(TemplateView):
    template_name = 'home.html'

    # override get context date method
    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()
        context['clients'] = Client.objects.all()
        return context

    def index(self, request):
        if request.method == 'POST':
            message = request.POST['message']
            sender = request.POST['email']

            send_mail('Contact Form',
                      message,
                      sender,
                      [settings.EMAIL_HOST_USER],
                      fail_silently=False)
        return render(request, 'home.html')

每次我尝试提交时,都找不到405方法,我只是不明白如何正确设置此方法才能按照我希望的方式工作。 有人有解决方案吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试过将get和post方法添加到CBV中?

from django.shortcuts import render
from django.views.generic import TemplateView
from .models import *
from django.core.mail import send_mail
from django.conf import settings


class HomeTemplateView(TemplateView):
    template_name = 'home.html'

    # override get context date method
    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()
        context['clients'] = Client.objects.all()
        return context

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

    def post(self, request):
        if request.method == 'POST':
            message = request.POST['message']
            sender = request.POST['email']

            send_mail('Contact Form',
                      message,
                      sender,
                      [settings.EMAIL_HOST_USER],
                      fail_silently=False)
        return render(request, 'home.html')