Python 为flask poll应用程序输入选项

Python 为flask poll应用程序输入选项,python,html,sqlite,flask,jinja2,Python,Html,Sqlite,Flask,Jinja2,我使用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 nu

我使用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模板中)并将选择插入数据库。下面是我的“显示投票”和“添加投票”

    @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 %}
{范围(4)%内的i的%
选择:
{%endfor%}

如果没有完整的模板或HTML,我将假定HTML
是有效的。看看你是否怀疑那里有问题

要验证表单值是否达到add_poll()函数,请尝试使用(即在
app.run()之前设置
app.debug=True
)。要强制调用调试器,请在add_poll()函数中插入错误,然后从浏览器再次提交表单。应显示回溯的副本。单击回溯最后一行中的“控制台”图标(这应该是您在add_poll()中创建的错误),并开始以交互方式检查request.form对象

[console ready]
>>> request.form
werkzeug.datastructures.ImmutableMultiDict({'choicetext': u''})
>>> str(request.form)
"ImmutableMultiDict([('choicetext', u'choice1'), ('choicetext', u'choice2'), ('choicetext', u'choice3'), ('choicetext', u'choice4')])"
>>> dir(request.form)
['KeyError', '__class__', '__cmp__', '__contains__', '__copy__', '__delattr__',    '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'clear', 'copy', 'fromkeys', 'get', 'getlist', 'has_key', 'items', 'iteritems', 'iterkeys', 'iterlists', 'iterlistvalues', 'itervalues', 'keys', 'lists', 'listvalues', 'pop', 'popitem', 'popitemlist', 'poplist', 'setdefault',         'setlist', 'setlistdefault', 'to_dict', 'update', 'values'  ]
>>> request.form.getlist('choicetext')
[u'choice1', u'choice2', u'choice3', u'choice4']
希望这将明确在add_poll()中必须更改的内容,并简化应用程序的未来调试。祝你好运

有关详细信息,请阅读和对象的文档。例如,在烧瓶内处理表单验证(管道安装到位后的下一步),这可能是一个很好的起点