
正在使用ponyorm尝试弄出一个基于flask的网站. 但是在处理 update 的时候发现怎么弄都弄不好.
处理 update 的 view 如下. 可是在实际情况中是点了 submit 之后, redirect 到的 index.html 页面上显示的数据信息根本就没有 update 到.
目前的 active 是 False, 我想在通过选择 active 到 True 然后点击 submit:
@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 个文件代码:
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) <!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> <!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> </dv> <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>