Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的django表单作为GET发送,然后在POST中给出405错误_Python_Django_Django Forms_Django Views - Fatal编程技术网

Python 为什么我的django表单作为GET发送,然后在POST中给出405错误

Python 为什么我的django表单作为GET发送,然后在POST中给出405错误,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我正在尝试用Django创建一个“更改电子邮件”表单。表单显示在模板中,但当我提交表单时,这种情况会发生在终端中 [09/Feb/2016 09:19:55] "GET /change_email/ HTTP/1.1" 200 3775 [09/Feb/2016 09:19:55] "GET /static/css/styles.css HTTP/1.1" 304 0 [09/Feb/2016 09:20:09] "POST /account/ HTTP/1.1" 405 0 然后发送到一个空

我正在尝试用Django创建一个“更改电子邮件”表单。表单显示在模板中,但当我提交表单时,这种情况会发生在终端中

[09/Feb/2016 09:19:55] "GET /change_email/ HTTP/1.1" 200 3775
[09/Feb/2016 09:19:55] "GET /static/css/styles.css HTTP/1.1" 304 0
[09/Feb/2016 09:20:09] "POST /account/ HTTP/1.1" 405 0
然后发送到一个空白页。我还查看了视图并注意到,对于一些打印语句,它将转到最后一个else语句,而不是通过
if request。POST:

这是我的密码:

视图:

表格:

模板:

<form action="{% url 'account' %}" method="post">
    {% csrf_token %}
    <div class="form-group">
        <label for="Current Email">Current Email:</label>
        <div class="col-sm-10">
            {% render_field form.current_email type="email" class+="form-control" placeholder="Current Email" %}
        </div>
    </div>
    <div class="form-group">
         <label for="New Email">New Email:</label>
         <div class="col-sm-10">
             {% render_field form.new_email type="email" class+="form-control" placeholder="New Email" %}
         </div>
    </div>
    <div class="form-group">
         <label for="Confirm New Email">Confirm New Email:</label>
         <div class="col-sm-10">
             {% render_field form.confirm_email type="email" class+="form-control" placeholder="Confirm New Email" %}
         </div>
    </div>
    <div class="form-group" style="padding-top: 40px; text-align:right">
       <button type="submit" class="btn btn-primary">Save changes</button>
     </div>
 </form>    
这将把表单发送回change_电子邮件,并通过POST方法部分(当前发送到另一个url)。此外,使用

if request.method == 'POST':
这将把表单发送回change_电子邮件,并通过POST方法部分(当前发送到另一个url)。此外,使用

if request.method == 'POST':

TemplateView
将不会定义
post
方法。它是一个基本类,使模板渲染变得容易

如果没有为相应的HTTP谓词定义适当的方法,Django将在基于类的视图上抛出
405 METHOD NOT ALLOWED

您应该实现自己的类:

class AccountView(TemplateView):
    template_name='account_details.html'

    def post(self):
        # your code to handle HTTP post
        # return response

    def get(self):
        # your code to handle HTTP get
        # return response
并以不同的方式:

url(r'^account/$',  AccountView.as_view()),

有关详细信息,请访问:

TemplateView
将不定义
post
方法。它是一个基本类,使模板渲染变得容易

如果没有为相应的HTTP谓词定义适当的方法,Django将在基于类的视图上抛出
405 METHOD NOT ALLOWED

您应该实现自己的类:

class AccountView(TemplateView):
    template_name='account_details.html'

    def post(self):
        # your code to handle HTTP post
        # return response

    def get(self):
        # your code to handle HTTP get
        # return response
并以不同的方式:

url(r'^account/$',  AccountView.as_view()),

更多信息请访问:

向我们展示您的
url.py
,不允许使用方法405,这不是来自您发布的视图。同样值得怀疑的是,您的表单正在发布到url“account”,而该视图被称为“change\u email”。你确定这是正确的视图吗?这是我的url.py:
urlpatterns=[url(r'^change\u email/$,views.change\u email,name=“change\u email”),url(r'^account/$”,TemplateView.as\u view(template\u name='account\u details.html'),name=“account”),][/code>抱歉,我现在已经添加了注意,使用转义数据更安全:email=form.cleaned\u data['current_email']向我们显示您的
url.py
,please.405是不允许的方法,它不是来自您发布的视图。您的表单是否正在发布到url“帐户”,而视图名为“change_email”。您确定这是正确的视图吗?这是我的url.py:
urlpatterns=[url](r'^change\u email/$,views.change\u email,name=“change\u email”),url(r'^account/$,TemplateView.as\u view(template\u name=“account\u details.html'),name=“account”),]
对不起,我现在已经添加了注意,使用转义数据更安全:email=form.cleaned\u data['current\u email']
url(r'^account/$',  AccountView.as_view()),