flask, 如何处理 ponyorm 中的 update? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
suueyoung

flask, 如何处理 ponyorm 中的 update?

  •  
  •   suueyoung Sep 5, 2016 3641 views
    This topic created in 3531 days ago, the information mentioned may be changed or developed.

    正在使用ponyorm尝试弄出一个基于flask的网站. 但是在处理 update 的时候发现怎么弄都弄不好.

    处理 update 的 view 如下. 可是在实际情况中是点了 submit 之后, redirect 到的 index.html 页面上显示的数据信息根本就没有 update 到.

    目前的 active 是 False, 我想在通过选择 active 到 True 然后点击 submit:

    1. 把 active 保存到数据库;
    2. 之后重定向到 index.html.
    @app.route('/update/<int:id>', methods=('GET', 'POST')) @db_session def update(id): record = Tconfused.get(lambda p: p.id == id) form = UpdateForm() form.id.data = record.id form.active.data = record.active form.url.data = record.url if request.method == 'POST' and form.validate_on_submit(): record.active = form.active.data record.url = form.url.data.strip() return redirect(url_for('index')) return render_template('update.html', record=record, form=form) 

    从 f12 看见的 form data 也都没有错误

    csrf_token:1473071404##a662b5cd7428bb709b74372a0d12eed2d9c26b32 active:1 url:http://www.google.com 

    请问, 要达到我的目的, 这个 view 里面有关数据库操作的部分要怎么写呢? 这里是完整的 3 个文件代码:

    • confused.py
    • templates/index.html
    • templates/update.html
    confused.py
    import os from pony.orm import Database, db_session, Required, Optional from flask import Flask, redirect, url_for, render_template, request from flask_wtf import Form from wtforms import SelectField, StringField, IntegerField from wtforms.validators import DataRequired ABS_DB_PATH = os.path.basename(__file__) + 'confuse.db' db = Database() app = Flask(__name__) app.config['SECRET_KEY'] = 'confused' class Tconfused(db.Entity): active = Required(bool, default=0) url = Optional(str) class UpdateForm(Form): id = IntegerField('ID') active = SelectField('Active', choices=[(1, 'True'), (0, 'False')]) url = StringField('URL', validators=[DataRequired('url needed.')]) @app.route('/') @db_session def index(): records = Tconfused.select()[:] return render_template('index.html', records=records) @app.route('/update/<int:id>', methods=('GET', 'POST')) @db_session def update(id): record = Tconfused.get(lambda p: p.id == id) form = UpdateForm() form.id.data = record.id form.active.data = record.active form.url.data = record.url if request.method == 'POST' and form.validate_on_submit(): record.active = form.active.data record.url = form.url.data.strip() return redirect(url_for('index')) return render_template('update.html', record=record, form=form) if __name__ == '__main__': db.bind('sqlite', ABS_DB_PATH, create_db=1) db.generate_mapping(create_tables=1) app.run(host='0.0.0.0', debug=1, port=5011) 
    index.html
    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <table> <thead> <tr> <td>edit</td> <td>id</td> <td>active</td> <td>url</td> </tr> </thead> <tbody> {% for record in records %} <tr> <td><a href="/update/{{ record.id }}">edit</a></td> <td>{{ record.id }}</td> <td>{{ record.active }}</td> <td>{{ record.url }}</td> </tr> {% endfor %} </tbody> </table> </div> </body> </html> 

    update.html

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <form action="" method="post"> {{ form.hidden_tag() }} <div>{{ form.id.label(disabled=True) }} {{ form.id.data }}</div> <div>{{ form.active.label }} {{ form.active() }}</div> <div>{{ form.url.label }} {{ form.url(size=50) }}</div> <div><input type="submit"></div> </form> </div> <br> <div> <table> <thead> <tr> <td>id</td> <td>active</td> <td>url</td> </tr> </thead> <tbody> <tr> <td>{{ record.id }}</td> <td>{{ record.active }}</td> <td>{{ record.url }}</td> </tr> </tbody> </table> </div> </body> </html> 
    2 replies    2016-09-05 21:11:11 +08:00
    bjjvvv
    &nbs;   1
    bjjvvv  
       Sep 5, 2016
    form = UpdateForm()
    form.id.data = record.id
    form.active.data = record.active
    form.url.data = record.url

    这 4 句 POST 请求的时候同样会执行的,所以你把 POST 来的数据又赋值成原来的数据了
    所以你要把这 4 句放到 if 块的下面
    suueyoung
        2
    suueyoung  
    OP
       Sep 5, 2016
    @bjjvvv 太感谢了. 搞定了!
    About     Help     Advertise     Blog     API     FAQ     Solana     2987 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 15:17 PVG 23:17 LAX 08:17 JFK 11:17
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86