gpt4 book ai didi

flask - 编辑表格不起作用

转载 作者:行者123 更新时间:2023-12-03 17:03:57 26 4
gpt4 key购买 nike

圣诞快乐!!!我使用 flask sqlalchemy 和 wtf。我可以创建新的并显示在 info.html 但是当我尝试编辑表单时,数据库中没有变化所以它不起作用。所以我想知道问题在哪里?

app.py


    #With this route I can add new form in database 
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate()== True:
contact = Contacts()
# populate the model
form.populate_obj(contact)
db.session.add(contact)
db.session.commit()
# Contact commited to database
# RETURN TO INFO.HTML
return redirect(url_for('info'))

else:
#If the form does not have all fields that are required
return render_template('contact.html', form=form)

# This is the part for edit which is not working
# so I query and populate it but no change none in
# database none in info.html
@app.route('/edit/<int:id>', methods=['POST'])
def edit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate_on_submit()== True:
#populate it
form.populate_obj(user)
db.session.commit()
return redirect(url_for('info'))
else:
#If the form does not have all fields that are required
return render_template('edit.html', id=id )
@app.route('/edit/<int:id>', methods=['GET'])
def profile(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form, obj=user)
return render_template('edit.html',form=form, id =id)
# this route to html that should show all info
@app.route('/info', methods=['GET', 'POST'])
def info():
#query all
info = Contacts.query.all()
return render_template('info.html', contact=info)

model.py


 # model with table name Contacts
class Contacts(db.Model):
__tablename__ = "Contacts"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
age = db.Column(db.Integer)

form.py


# this is the form wtf
Class LoginForm(Form):
name = StringField("Name", [validators.Required("Please enter your name.")])
email = StringField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter valid email address.")])
age = IntegerField("age", [validators.Required("Please enter your age.")])
submit = SubmitField("Submit")

info.html


 # it should display all updated form But it wont??
{% extends "layout.html" %}
{% block content %}
<h2>show the info</h2>
{% for contact in contact %} # Maybe this is also Problem?
<strong>name:</strong> {{ contact.name}} <br>
<strong>email:</strong> {{ contact.email }} <br>
<strong>age:</strong> {{ contact.age}} <br>
<br>
{% endfor %}

{% endblock %}

最佳答案

当您每次查询获取对象时,您不必读取对象。 Queryobject 绑定(bind)到 session 。所以你只需要commit更改而不是 add他们再次。

所以这应该是更正的代码片段

user = Contacts.query.get_or_404(id)  
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate()== True:
# populate it
form.populate_obj(user)
db.session.commit()

关于flask - 编辑表格不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com