Django - 提交联系表时出现 405 错误
Django - Getting 405 error when submitting contact form
我的 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 视图 class
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 方法未找到,我只是不明白如何正确设置它才能按我想要的方式工作。
有人有解决办法吗?
您是否尝试过将 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')
我的 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 视图 class
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 方法未找到,我只是不明白如何正确设置它才能按我想要的方式工作。 有人有解决办法吗?
您是否尝试过将 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')