【问题标题】:Cannot pass arguments from html to view function无法将参数从 html 传递到视图函数
【发布时间】:2016-02-02 14:39:19
【问题描述】:

我已经为这个问题头疼了一天。

我正在使用 Django password_reset_confirm 视图来处理来自用户的密码更改请求。

我收到了screenshot 中所示的错误。

Reverse mismatch for 'password_reset_confirm'

这是我呈现表单的 html。

<div>
    <h1></h1>
    <form id='reset-password-form' action="{% url 'password_reset_confirm'  uidb64=uidb64 token=token %}" method="post">
        {% csrf_token %}
        {{form|foundation }}
        {{title}}
        {{validlink}}
        <div id='submit-button'>
            <input type="submit" id='submit-reset-form'>
        </div>
    </form>
</div>

url映射是这样的:

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',password_reset_confirm, {'set_password_form':ResetPasswordForm,'template_name':'email/password_reset/password_enternew_password.html',}
    ,name='password_reset_confirm')

谁能告诉我,错误在哪里?

【问题讨论】:

    标签: html django django-views django-urls


    【解决方案1】:

    uidb64token 不会传递到模板上下文,因此您不能在 url 标记中使用它们。

    最简单的解决方法是从表单中删除 action 属性。这样可以防止反向匹配错误,表单会提交到当前的url,是正确的。

    如果您查看 master 分支中 django.contrib.admin 应用程序中包含的 password reset confirm template 的代码,您会发现它没有操作属性。早期版本的 Django 有action="",但这在 HTML5 中无效。

    【讨论】:

    • 但是 password_reset_confirm 将 uidb64 和 token 作为参数,不应该从模板本身传递参数吗?
    • 根据文档,password_reset_confirm 可以从点击的链接中获取用户信息,并给出一个很好的参考链接。
    • 对不起,我想我之前误解了你的问题。请参阅我的更新答案。
    • 如果form标签中没有添加action属性,怎么提交表单?.
    • 如果表单没有action属性,它会自动提交到当前url(已经包含uidb64token)。
    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2017-05-12
    相关资源
    最近更新 更多