flask-sqlalchemy mysql,Flask-SQLAlchemy更新正在MySQL中创建新记录

I am trying to update a MySQL record from flask-sqlalchemy, but instead of updating the current record, it is adding a new one. But here's the really weird thing, when I run the same code from the Python prompt it updates properly without adding a new record.

Here is the relevant portion of the code:

First, the template that feeds the blog:

{% for post in posts %}

{{ post.title|safe }}

{{ post.content|safe }}

{{ post.posted.strftime('%d %B %Y %I:%M')|safe }}

{% if session.logged_in %}

{% endif %}

{% endfor %}

Now, the code for the views:

@app.route('/blog', methods = ['GET', 'POST'])

def blog():

# Get all records from database blog table:

posts = models.Blog.query.order_by(models.Blog.posted.desc()).all()

context = {

'copyright': COPYRIGHT,

'posts': posts

}

if request.method == 'POST':

blog_id = request.form.get('blog_id')

if request.form.get('submit') == 'Edit Post':

return redirect(url_for('edit_entry', blog_id = blog_id))

elif request.form.get('submit') == 'Delete Post':

pass

elif request.method == 'GET':

return render_template('blog.html', **context)

@app.route('/edit_entry', methods = ['GET', 'POST'])

def edit_entry():

form = BlogEntry() # A WTForms object.

# blog_id is passed from a redirect. Tested and ensured it is passing proper value.

blog_id = request.args['blog_id']

post = models.Blog.query.filter_by(id = blog_id).one()

if request.method == 'POST':

post.title = form.title.data

post.content = form.content.data

db.session.commit()

return redirect(url_for('blog'))

elif request.method == 'GET':

context = {

'copyright': COPYRIGHT,

'form': form,

}

form.title.data = post.title

form.content.data = post.content

return render_template('edit_entry.html', **context)

This is the code as it is right now in my app file. I've run the exact same code from the Python prompt in the terminal, and it works perfectly, but when this code runs from the app file, it creates a new record instead of updating the existing one.

As a note, I have also tried:

models.Blog.query.filter_by(id=blog_id).update({

'title': form.title.data,

'content': form.content.data

})

db.session.commit()

and by replacing models.Blog with db.session.query(models.Blog). Basically, i've tried every method and combination of code for updating records using flask-sqlalchemy that I could find on this forums and others. All the same issue. When I run them from the python prompt, they update the correct record in the database. When I run them from the app, they create a new record.

If you want to see more of the code, you can check the entire project here: https://github.com/tbellerose/lauraopfelphotography.com

解决方案

Okay, so thanks to Daniel and Doobeh for pointing me in the right directions. Basically it came down to me not requesting the blog_id properly in the POST method of edit_entry. Here is the new (and working) code.

def edit_entry():

form = BlogEntry()

if request.method == 'POST':

blog_id = request.form.get('blog_id')

update = db.session.query(models.Blog).filter_by(id = blog_id).update({

'title': request.form.get('title'),

'content': request.form.get('content')

})

db.session.commit()

return redirect(url_for('blog'))

elif request.method == 'GET':

blog_id = int(request.args['blog_id'])

post = models.Blog.query.filter_by(id = blog_id).first_or_404()

context = {

'copyright': COPYRIGHT,

'form': form,

'blog_id': blog_id

}

form.title.data = post.title

form.content.data = post.content

return render_template('edit_entry.html', **context)

There ended up being two major issues. Fist was an oversight: when I copied the edit_entry template from the new_entry template, I forgot to change the action of the form, so the form was actually posting to the new_entry route, thus the duplication. After I found that problem, I also realized that while blog_id was passed in request.args to the 'GET' method, it wasn't being passed in the 'POST' method (since post wasn't coming from the redirect), so I actually created a new field in the edit_entry template to pass the blog_id back to POST.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值