Django python联系电子邮件表单错误帖子不允许405
Posted
技术标签:
【中文标题】Django python联系电子邮件表单错误帖子不允许405【英文标题】:Django python contact email form error post not allowed 405 【发布时间】:2018-02-23 23:24:03 【问题描述】:我不知道为什么我在 bash 中收到此错误:
方法不允许 (POST): /curriculum/ [14/Sep/2017 20:47:24] "POST /curriculum/ HTTP/1.1" 405 0
views.py:
from django.views.generic import TemplateView from Profile.forms import ContactForm from django.core.mail import send_mail, BadHeaderError, EmailMessage from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render, redirect from django.template import Context from django.template.loader import get_template
# Create your views here. class HomePageView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context=None)
class ProjectsPageView(TemplateView):
template_name = 'projects.html'
class TutorialsPageView(TemplateView):
template_name = 'tutorials.html'
class ArticlesPageView(TemplateView):
template_name = 'articles.html'
class LanguagesPageView(TemplateView):
template_name = 'languages.html'
class VideosPageView(TemplateView):
template_name = 'videos.html'
class CurriculumPageView(TemplateView):
template_name = 'curriculum.html'
def post(self, request, **kwargs):
form_class = ContactForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = request.POST.get(
'contact'
, '')
contact_email = request.POST.get(
'email'
, '')
form_content = request.POST.get('message', '')
# Email the profile with the
# contact information
template = get_template('templates/contact_template.txt')
context = Context(
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
)
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['juanmacedoal@gmail.com'],
headers = 'Reply-To': contact_email
)
email.send()
return redirect('curriculum')
return render(request, 'PageJMMA/Profile/templates/index.html',
'form': form_class,
)
url.py:
from django.conf.urls import url from Profile import views
urlpatterns = [
url(r'^curriculum/$', views.CurriculumPageView.as_view(), name='curriculum') ]
forms.py:
from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(
required=True,
widget=forms.Textarea
)
curriculum.html(仅表单部分):
<div class="w3-col m6">
<form method="POST" action="" role="form">
<div class="w3-row-padding" style="margin:0 -16px 8px -16px">
<div class="w3-half">
<input class="w3-input w3-border" type="text" placeholder="Name" required name="contact">
</div>
<div class="w3-half">
<input class="w3-input w3-border" type="text" placeholder="Email" required name="email">
</div>
</div>
<input class="w3-input w3-border" type="text" placeholder="Message" required name="message">
% csrf_token %
form.as_p
<button class="w3-button w3-black w3-section w3-right" type="submit">SEND</button>
</form>
</div>'
【问题讨论】:
【参考方案1】:您的 post 函数应该在 TemplateView 内以覆盖 TemplateView 的默认帖子。 Generic TemplateView 不应该像这样使用,但无论如何覆盖 post 方法应该可以工作。
class CurriculumPageView(TemplateView):
template_name = 'curriculum.html'
def post(self, request, **kwargs):
form_class = ContactForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = request.POST.get(
'contact'
, '')
contact_email = request.POST.get(
'email'
, '')
form_content = request.POST.get('message', '')
# Email the profile with the
# contact information
template = get_template('templates/contact_template.txt')
context = Context(
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
)
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['juanmacedoal@gmail.com'],
headers = 'Reply-To': contact_email
)
email.send()
return redirect('curriculum')
return render(request, 'PageJMMA/Profile/templates/index.html',
'form': form_class,
)
另外我建议使用FormView
【讨论】:
以上是关于Django python联系电子邮件表单错误帖子不允许405的主要内容,如果未能解决你的问题,请参考以下文章
Django 联系电子邮件表单未在模板 home.html 中呈现/显示
错误:SMTPRecipientsRefused 553, '5.7.1 #while 在 django 中处理联系表单