2013-05-13 2 views
5

저는 Flask를 사용하여 포럼 프로젝트를 만들고 Flask-SQLAlchemy를 사용하여 모든 사용자, 스레드, 게시물 등을 관리합니다. 그러나 x (예 : 게시물 수정)를 시도 할 때 다른 작업 (예 : 게시물 삭제)을 시도하면 InvalidRequestError가 발생한다는 것을 발견했습니다. 게시물을 편집 할 Flask-SQLAlchemy InvalidRequestError : 개체가 이미 세션에 연결되어 있습니다.

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

+0

나는 "thr ="부분을 의미했습니다. 하지만 나는/thr = 44/p = 32/c = 21 종류의 URL 형식을 할 수 있다고 생각합니다. 그게 이상하다는 것을 알았습니다. 나는 우리가 문제를 알아 냈기 때문에 기쁘다. – Dexter

+0

그게 정확히 무엇입니까; 예를 들어 포럼 (1)의 특정 스레드 (8)가 "/ forum/id = 1/thr = 8"이라는 URL을 만듭니다. – Ganye

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)) 
관련 문제