2017-09-14 73 views
0

我不知道爲什麼我在bash中得到這個錯誤:Django的蟒蛇聯繫電子郵件形式的錯誤後不允許405

不允許的方法 (POST):/課程/ [14 /月/ 2017年20時47分24秒] 「POST /課程/ 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" +'', 
       ['[email protected]'], 
       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

你的職位功能應該是裏面TemplateView覆蓋TemplateView的違約後。泛型TemplateView並不意味着像這樣使用,但無論如何,覆蓋後的方法應該工作。

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" +'', 
        ['[email protected]'], 
        headers = {'Reply-To': contact_email } 
       ) 
       email.send() 
       return redirect('curriculum') 

     return render(request, 'PageJMMA/Profile/templates/index.html', { 
      'form': form_class, 
     }) 

此外,我會建議使用,而FormView