V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Tianny
V2EX  ›  Python

初学 flask,问个问题,绕了半天了。。

  •  
  •   Tianny · 2017-07-04 00:09:47 +08:00 · 2184 次点击
    这是一个创建于 2503 天前的主题,其中的信息可能已经有所发展或是发生改变。

    先上代码,这个是 routing 和 view

    @auth.route('/change-password', methods=['GET', 'POST'])
    @login_required
    def change_password():
        form = ChangePasswordForm()
        if form.validate_on_submit():
            if current_user.verify_password(form.old_password.data):
                current_user.password = form.password.data
                db.session.add(current_user)
                flash('Your password has been updated.')
                return redirect(url_for('main.index'))
            else:
                flash('Invalid password.')
        return render_template("auth/change_password.html", form=form)
    

    这个是 change_password.html 的模板文件

    {% extends "base.html" %}
    {% import "bootstrap/wtf.html" as wtf %}
    
    {% block title %}Flasky - Change Password{% endblock %}
    
    {% block page_content %}
    <div class="page-header">
        <h1>Change Your Password</h1>
    </div>
    <div class="col-md-4">
        {{ wtf.quick_form(form) }}
    </div>
    {% endblock %}%
    

    这个是各个模板的父模板 base.html 中的相关部分

    <li><a href="{{ url_for('auth.change_password') }}">Change Password</a></li>
    

    上面实现的功能是,已登录用户如果点击 Change Password,链接就会跳转到 /change-password,跳出一个重置密码的表单,点完提交按钮后,application 然后再执行对应的 view function。

    我的问题是:当点击 Change Password 后,跳转到 /change-password 地址后,会展现一个重置密码的表单,我没理解的地方是,按照路由映射关系,/change-password 这个 url 交由 change_password()这个视图处理,按照函数执行的顺序,先引用表单即 form=ChangePasswordForm(),然后更新密码,最后返回一个重定向到主页,就结束了。这样的话,那么return render_template("auth/change_password.html", form=form)这段代码压根就没执行到,都没有返回渲染后的模板文件,又怎么有了前面点击 chang password 就蹦出一个重置密码表单的页面呢?

    这里我给绕晕了,求解释下,谢谢大家

    10 条回复    2017-07-04 18:56:53 +08:00
    coolair
        1
    coolair  
       2017-07-04 00:22:52 +08:00 via Android   ❤️ 1
    点了 submit 才会重定向到 index,没点就显示这个页面,注意有个 if。
    troywinter
        2
    troywinter  
       2017-07-04 00:23:35 +08:00   ❤️ 1
    你点击 chang_password 时是一个 get 请求,所以 if 那块的代码根本不会执行,执行的只是 render_template 那块的代码,想看到底执行没执行打个断点看看,就明白了
    welkinzh
        3
    welkinzh  
       2017-07-04 00:50:35 +08:00 via Android   ❤️ 1
    点击 submit 后才会跳转到 index 不然渲染的是 change passwd 页面呀
    kimchan
        4
    kimchan  
       2017-07-04 09:41:07 +08:00   ❤️ 1
    if form.validate_on_submit()
    Tianny
        5
    Tianny  
    OP
       2017-07-04 10:32:24 +08:00
    @coolair 感谢
    Tianny
        6
    Tianny  
    OP
       2017-07-04 10:32:44 +08:00
    @troywinter 厉害了 一阵见血
    Tianny
        7
    Tianny  
    OP
       2017-07-04 10:32:54 +08:00
    @kimchan thx
    Tianny
        8
    Tianny  
    OP
       2017-07-04 10:33:07 +08:00
    xvx
        9
    xvx  
       2017-07-04 16:59:49 +08:00 via iPhone
    GET 请求和 POST 请求,执行的是不同的代码。打开的时候是 GET 请求,返回的是 change_password.html 页面,点击提交的时候是 POST 请求,才会执行 validate_on_submit()后面的代码。
    Tianny
        10
    Tianny  
    OP
       2017-07-04 18:56:53 +08:00
    @xvx 恩 知道了 thx !
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5585 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:51 · PVG 14:51 · LAX 23:51 · JFK 02:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.