2013-05-13 118 views
5

我創建使用瓶的論壇項目,並管理所有用戶,主題,帖子等。然而,我發現,當我嘗試做X(例如編輯後),我得到一個InvalidRequestError如果我嘗試做別的事(例如刪除後)。燒瓶SQLAlchemy的InvalidRequestError:目的是使用燒瓶SQLAlchemy的已經連接到會議

進行編輯後,

def post_edit(id, t_id, p_id): 
    post = Post.query.filter_by(id=p_id).first() 
    if post.author.username == g.user.username: 
    form = PostForm(body=post.body) 
    if form.validate_on_submit(): 
     post.body = form.body.data 
     db.session.commit() 
     return redirect(url_for('thread', id=id, t_id=t_id)) 
    return render_template('post_edit.html', form=form, title='Edit') 
    else: 
    flash('Access denied.') 
    return redirect(url_for('thread', id=id, t_id=t_id)) 

和刪除後,

@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/delete', methods=['GET','POST']) 
def post_delete(id, t_id, p_id): 
    post = Post.query.filter_by(id=p_id).first() 
    if post.author.username == g.user.username: 
    db.session.delete(post) 
    db.session.commit() 
    return redirect(url_for('thread', id=id, t_id=t_id)) 
    else: 
    flash('Access denied.') 
    return redirect(url_for('thread', id=id, t_id=t_id)) 

和張貼後

@app.route('/forum/id=<id>/thr=<t_id>', methods=['GET','POST']) 
def thread(id, t_id): 
    forum = Forum.query.filter_by(id=id).first() 
    thread = Thread.query.filter_by(id=t_id).first() 
    posts = Post.query.filter_by(thread=thread).all() 
    form = PostForm() 
    if form.validate_on_submit(): 
    post = Post(body=form.body.data, 
       timestamp=datetime.utcnow(), 
       thread=thread, 
       author=g.user) 
    db.session.add(post) 
    db.session.commit() 
    return redirect(url_for('thread', id=id, t_id=t_id)) 
    return render_template('thread.html', forum=forum, thread=thread, posts=posts, form=form, title=thread.title) 

不幸的是,唯一正確的方式,使這個問題解決本身就是重置實際運行應用程序的腳本,run.py

#!bin/python 

from app import app 
app.run(debug=True,host='0.0.0.0') 

回答

3

您是否使用了WooshAlchemy,因爲它可能是您的問題的一部分。 Described here

他介紹,需要WooshAlchemy擴展的修改「修復」。

雖然通常這可能意味着你叫一個Post模型對象,然後使用「session.add」連接,然後試圖「執行Session.delete」或做了另外一個「session.add」對同一個對象。

另外你的請求路由對於燒瓶有點奇怪我以前從未見過「thr = <t_id>」類型的符號。這對你來說工作得很好嗎?

http://flask.pocoo.org/docs/quickstart/#variable-rules

+0

看起來像WhooshAlchemy的確是問題所在。至於符號,它只是一個簡寫「THR = 」。 – Ganye 2013-05-13 21:15:25

+0

我的意思是「thr =」部分。但我想你可以做/ thr = 44/p = 32/c = 21種url格式,我只是覺得很奇怪。我很高興我們發現了這個問題。 – Dexter 2013-05-14 06:08:41

+0

就是這樣;例如,論壇(1)中的特定主題(8)會創建url「/ forum/id = 1/thr = 8」。 – Ganye 2013-05-15 18:59:36

0

我覺得你的編輯後沒有正確完成。使用populate_obj函數。

@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/edit', methods=['GET','POST']) 
def post_edit(id, t_id, p_id): 
    post = Post.query.filter_by(id=p_id).first() 
    if post.author.username == g.user.username: 
     form = PostForm(obj=post) 
     if form.validate_on_submit(): 
      form.populate_obj(post) 
      db.session.commit() 
      return redirect(url_for('thread', id=id, t_id=t_id)) 
     return render_template('post_edit.html', form=form, title='Edit') 
    else: 
     flash('Access denied.') 
     return redirect(url_for('thread', id=id, t_id=t_id))