为 Flask 民意调查应用程序输入选择
我已经为使用 Flask 制作的民意调查应用程序创建了一个数据库架构,如下所示:
CREATE TABLE questions (
question_id integer primary key autoincrement,
questiontext string not null
);
CREATE TABLE choices (
choice_id integer primary key autoincrement,
choicetext string not null,
question_id integer,
FOREIGN KEY(question_id) REFERENCES questions(question_id)
);
但我不知道应该如何询问(在 HTML 模板中)并将选择插入数据库。我的“show_polls”和“add_polls”在下面
@app.route('/')
def show_polls():
cur = g.db.execute('SELECT questiontext, choicetext FROM questions q JOIN choices c ON c.question_id = q.question_id')
polls = [dict(question=row[0], choices=(c for c in row[1:])) for row in cur.fetchall()]
return render_template('show_polls.html', polls=polls)
@app.route('/add', methods=['POST'])
def add_poll():
if not session.get('logged_in'):
abort(401)
g.db.execute('insert into questions (questiontext) values (?)',
[request.form['questiontext']])
for i in range(4): #4 choices
g.db.execute('insert into choices (choicetext, question_id) values(?, ?)',
[request.form['choicetext'], 4])
g.db.commit()
return redirect(url_for('show_polls'))
但这不起作用。我不确定我的视图或 HTML 布局部分是否错误。有人可以帮我解决这个问题吗?
以下是添加民意调查的 HTML 部分:
{% for i in range(4) %}
<dt>Choices:
<dd><input type=text name=choicetext>
{% endfor %}
I have made a database schema for a poll app I am making using Flask as follows:
CREATE TABLE questions (
question_id integer primary key autoincrement,
questiontext string not null
);
CREATE TABLE choices (
choice_id integer primary key autoincrement,
choicetext string not null,
question_id integer,
FOREIGN KEY(question_id) REFERENCES questions(question_id)
);
But I couldn't figure out how I should ask(in the HTML template) and insert the choices into the database. My 'show_polls' and 'add_polls' are below
@app.route('/')
def show_polls():
cur = g.db.execute('SELECT questiontext, choicetext FROM questions q JOIN choices c ON c.question_id = q.question_id')
polls = [dict(question=row[0], choices=(c for c in row[1:])) for row in cur.fetchall()]
return render_template('show_polls.html', polls=polls)
@app.route('/add', methods=['POST'])
def add_poll():
if not session.get('logged_in'):
abort(401)
g.db.execute('insert into questions (questiontext) values (?)',
[request.form['questiontext']])
for i in range(4): #4 choices
g.db.execute('insert into choices (choicetext, question_id) values(?, ?)',
[request.form['choicetext'], 4])
g.db.commit()
return redirect(url_for('show_polls'))
But this doesn't work. I'm not sure whether I've got the views wrong or the HTML layout part. Can anyone help me with this please?
Here's the HTML part that adds the polls:
{% for i in range(4) %}
<dt>Choices:
<dd><input type=text name=choicetext>
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有完整的模板或 HTML,我将假设 HTML
要验证表单值是否到达您的 add_poll() 函数,请尝试使用 Flask 调试模式 (即在
app.run()
之前设置app.debug = True
)。要强制调用调试器,请在 add_poll() 函数中插入错误,然后从浏览器再次提交表单。应出现回溯的副本。单击回溯最后一行中的“控制台”图标(这应该是您在 add_poll() 中创建的错误)并开始交互式检查 request.form 对象。希望这将明确 add_poll() 中必须更改的内容并简化应用程序的未来调试。祝你好运!
有关更多信息,请阅读 Flask.request.form 上的文档以及对象。有关在 Flask 中处理表单验证的示例(管道到位后的下一步),此 关于表单验证的 Flask 模式文档可能是一个不错的起点。
Without the full template or HTML, I am going to assume the HTML
<form>
is valid. See HTML Forms and Inputs if you suspect a problem there.To verify that form values are reaching your add_poll() function, try using Flask debug mode (i.e. set
app.debug = True
prior toapp.run()
). To force invocation of the debugger, insert an error in the add_poll() function and submit the form again from a browser. A copy of the Traceback should appear. Click on the 'console' icon in the last line of the traceback (which should be the error you created inside add_poll()) and start interactively inspecting the request.form object.Hopefully this will make clear what must change in add_poll() and simplify future debug of your app. Good luck!
For more information, read the documentation on Flask.request.form and werkzeug.datastructures.MultiDict objects. For an example of handling form validation within Flask (the next step after the plumbing is in place), this Flask pattern document on Form Validation might be a good place to start.