Django发布请求返回405方法不允许错误

时间:2018-08-10 17:46:01

标签: python django python-3.x django-forms http-post

我正在处理Django(1.10)项目,在该项目中,我正在向视图发送发布请求,但它返回405 Method not allowed错误。

这是我的 urls.py:

urlpatterns = [
url('admin/', admin.site.urls),
url(r'$', TemplateView.as_view(template_name='Unmundonuevo/index.html')),
url(r'^contactUs/$', views.ContactFormView.as_view(), name='ContactUs'),

]

这是我的HTML表单:

<form method="post" action="{% url 'ContactUs' %}">
      {% csrf_token %}
      <div class="form-group">
           <input type="text" name="name" id="id_name" class="form-control input-field" placeholder="Name" required="">
           <input type="email" name="email" id="id_email" class="form-control input-field" placeholder="Email ID" required="">
           <input type="text" name="subject" id="id_subject" class="form-control input-field" placeholder="Subject" required="">
     </div>
     <textarea name="message" id="message" class="textarea-field form-control" rows="4" placeholder="Enter your message" required=""></textarea>
     <button type="submit" id="btn" value="Submit" class="btn center-block">Send message</button>

</form>

这是我的 forms.py:

class ContactForm(forms.ModelForm):
    class Meta:
        fields = ('name', 'email', 'subject', 'message')
        model = models.ContactModel

而且,这是我的 views.py:

class ContactFormView(CreateView):
    def post(self, request, *args, **kwargs):
        if request.method == 'POST':
            print('Get post req')
            form = ContactForm()
            if form.is_valid():
                print('Valid form')
                return 'Success'
            return 'failed'

它返回:

  

不允许使用方法(POST):/ contactUs /

     

[11 / Aug / 2018 05:47:58]“ POST / contactUs / HTTP / 1.1” 405 0

请帮帮我!

谢谢!

0 个答案:

没有答案