我不知道为什么我在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>'
答案 0 :(得分:0)
您的帖子功能应该在TemplateView内部以覆盖TemplateView的默认帖子。通用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