【问题标题】:Method Not Allowed (POST): /password_change_done不允许的方法 (POST):/password_change_done
【发布时间】:2020-11-13 06:40:05
【问题描述】:

我是 django/python 的新手

我正在尝试更改用户密码,但出现错误

Method Not Allowed (POST): /password_change_done
Method Not Allowed: /password_change_done
[23/Jul/2020 19:11:05] "POST /password_change_done HTTP/1.1" 405 0   

这是我的 url 模式,仅用于更改密码:

 path('password_change',
    auth_views.PasswordChangeView.as_view(template_name='password_change.html'),
    name='password_change'),

    path('password_change_done',
    auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'),
    name='password_change_done'),

我的两个用于此操作的 html 页面 我的 html 代码:password_change

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
</head>
<body>
    <h3> Change Password</h3>
     
    <form action="{% url 'password_change_done' %}" method="POST" class="form-signin">{% csrf_token %}

        <input name="Old password" class="form_control" placeholder="Old password"
        type="password" id=old_password required="true">
        <input name="new password" class="form_control" placeholder="new password"
        type="password" id=new_password required="true">
        <input name="confirm password" class="form_control" placeholder="confirm password"
        type="password" id=confirm_password required="true">
        

        <button  type="submit">Update</button>
    </form>
</body>
</html>

password_chang_done 的 html 代码:

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome</h1>
    <h2> <img src="{% static 'images/welcome.jpg'  %}"> </h2>
    
    <h5>hello,{{request.user.username}}</h5>
    <script>
        window.alert("password sucessfully changed")
    </script>


</body>
</html>

【问题讨论】:

  • 您应该向"{% url 'password_change' %}" 视图发出POST 请求。 password_change_done 是更改成功时的结果。
  • 分享你的 password_change_done 控制器。
  • @mursalin:我认为这些是来自django.contrib.auth.views 的那些,但具有更新的模板名称(在.as_view(..) 调用中)。
  • 将发布请求更改为 password_change 使我保持在同一页面上,并且不会将我重定向到下一页
  • 真的,如果您只想使用 bootstrap4 重新设置样式,请保存some pain

标签: python html django django-forms


【解决方案1】:

你应该向password_change端点发出POST请求,而不是password_change_done`端点,所以:

<form action="{% url 'password_change' %}" method="POST" class="form-signin">
    …
</form>

在 Django Web 开发中很常见,在 Web 开发中通常使用 GET 请求对第一次生成页面的同一视图进行 POST。

此外,PasswordChangeView 视图使用PasswordChangeForm [GitHub]

class PasswordChangeForm(SetPasswordForm):

    # …

    field_order = ['old_password', 'new_password1', 'new_password2']

&lt;input&gt; 元素的名称因此应该是old_passwordnew_password1new_password2

<input name="old_password" class="form_control" placeholder="Old password" type="password" id=old_password required="true">
<input name="new_password1" class="form_control" placeholder="new password" type="password" id="new_password" required="true">
<input name="new_password2" class="form_control" placeholder="confirm password" type="password" id=confirm_password required="true">

【讨论】:

  • 提交表单时没有任何反应。我错过了一些非常小的东西吗?
  • @AbhishekSharma:您确定表单名称有效吗?如果在模板中渲染{{ form.errors }} 会怎样?
  • @AbhishekSharma:因此您需要正确指定 input 元素的名称,请参阅编辑。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2020-06-17
  • 2014-05-23
  • 1970-01-01
  • 2021-02-09
  • 2020-06-18
  • 2017-09-17
  • 2021-07-07
相关资源
最近更新 更多