2013-05-13 33 views
5

Tôi đang tạo một dự án diễn đàn bằng Flask và quản lý tất cả người dùng, chủ đề, bài đăng, v.v. sử dụng Flask-SQLAlchemy. Tuy nhiên, tôi thấy rằng khi tôi cố gắng làm x (ví dụ: chỉnh sửa bài đăng), tôi nhận được một InvalidRequestError nếu tôi cố gắng làm bất kỳ điều gì khác (ví dụ: xóa bài đăng).Flask-SQLAlchemy InvalidRequestError: Đối tượng đã được đính kèm vào phiên

Để biên tập một bài đăng,

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)) 

và xóa một bài đăng,

@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)) 

và đăng một bài

@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) 

Thật không may, cách chắc chắn duy nhất để làm vấn đề quyết tâm này chính nó là đặt lại tập lệnh thực sự chạy ứng dụng, run.py

#!bin/python 

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

Trả lời

3

Bạn đang sử dụng WooshAlchemy vì nó có thể là một phần của sự cố của bạn. Described here

Ông mô tả "sửa lỗi" yêu cầu sửa đổi phần mở rộng WooshAlchemy.

Thông thường mặc dù nó có thể có nghĩa là bạn đã gọi đối tượng Mô hình bài đăng và sau đó đính kèm nó bằng cách sử dụng "session.add" và sau đó thử "session.delete" hoặc thực hiện một "session.add" khác trên cùng một đối tượng.

Ngoài ra yêu cầu định tuyến của bạn hơi lạ đối với bình tôi chưa bao giờ thấy "thr = <t_id>" loại ký hiệu với Flask trước đây. Điều đó có hiệu quả với bạn không?

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

+0

Có vẻ như WhooshAlchemy thực sự là vấn đề. Đối với các ký hiệu, nó chỉ là một cách viết tắt cho "thr = ". – Ganye

+0

Tôi có nghĩa là phần "thr =". Nhưng tôi đoán bạn có thể làm/thr = 44/p = 32/c = 21 loại định dạng url, tôi chỉ thấy nó lạ là tất cả. Tôi rất vui vì chúng tôi đã tìm ra được vấn đề. – Dexter

+0

Chính xác đó là gì; ví dụ, một chủ đề cụ thể (8) trong một diễn đàn (1) tạo ra url "/ forum/id = 1/thr = 8". – Ganye

0

Tôi nghĩ rằng bài chỉnh sửa của bạn không được thực hiện đúng. Sử dụng hàm 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)) 
Các vấn đề liên quan