[英]how to put data(id) in form (wtforms) and get it back on submit
I am new to wtforms, so I`ll really appreciate any help. 我是wtforms的新手,所以我将非常感谢您的帮助。
I need to display books names and near each put "Delete" button. 我需要显示书籍名称,并在每个放置“删除”按钮的位置附近显示。 I`ve read wtforms crash course but I have no idea how to solve my problem with it.
我读过wtforms崩溃课程,但是我不知道如何解决它的问题。
So I decided to do it other way - my best idea is to render to template a dict with id and name and on submit return id, but I still can`t do it. 因此,我决定以其他方式进行操作-我最好的主意是将具有ID和名称的dict渲染为模板,并在提交返回ID时将其渲染为模板,但是我还是做不到。 Code examples are below.
代码示例如下。
It`s view.py 它的view.py
@application.route('/delete', methods=['GET', 'POST'])
def delete():
books_lib = mydatabase.get_all_books()
form = DeleteForm(request.form)
if request.method == 'POST':
delete_id = form.id.data
mydatabase.del_book(delete_id)
return render_template('delete.html', form = form, books_lib = books_lib)
return render_template('delete.html', form = form, books_lib = books_lib)
It`s template 它的模板
<!DOCTYPE html>
<html>
<body>
<h2>book to delete</h2>
<form action="/delete" name="delete" method="POST">
{% if books_lib %}
{% for id, book in books_lib.items() %}
<p>{{book}}:<input type="submit" value="Delete" id="{{id}}">
{% endfor%}
{% endif %}
</form>
</body>
</html>
It`s form 它的形式
class DeleteForm(Form):
book = TextField("Book name", [validators.Length(min=2, max=25)])
id = HiddenField("id")
Here is how I do it. 这是我的方法。
TEMPLATE 模板
<p><a href="{{ url_for('delete_post', url=post.url) }}">delete</a></p>
View 视图
# delete a post
@app.route('/<path:url>/d')
@login_required
def delete_post(url):
post = Post.get_post(url)
if post is None:
flash('post not found')
return redirect(url_for('home'))
db.session.delete(post)
db.session.commit()
Topic.update_counts()
flash('Your post has been deleted')
return redirect(url_for('index'))
the url_for is a Flask function that generates a url and binds it to a function. url_for是一个Flask函数,它生成一个url并将其绑定到一个函数。
Soooo.... in a nutshell. 太...简而言之。
to answer your original question.... 回答您的原始问题。
if you simply want to pass a list of ids to your template.... 如果您只想将ID列表传递给模板...。
First, create the display function. 首先,创建显示功能。
@app.route('/index')
def index():
posts = Post.query.order_by(Post.pub_date.desc()) #SQLAlchemy query.
return render_template('index.html', posts=posts) #sends all posts to template.
Now in your template you can generate a list of post.ids. 现在,您可以在模板中生成post.id列表。
{% for post in posts %}
{{post.id})
{% endfor %}
to add a delete link next to each item in your list use url_for. 要在列表中的每个项目旁边添加删除链接,请使用url_for。
{% for post in posts %}
<p>{{post.id}) <a href="{{ url_for('delete_post', id=post.id) }}">delete</a></p>
{% endfor %}
In this case I use post id instead of the post.url to identify the specific post I want to delete, so don't forget to modify your @app.route in your delete function. 在这种情况下,我使用post id而不是post.url来标识要删除的特定帖子,因此不要忘记在delete函数中修改@ app.route。
@app.route('/<int:id>/delete')
Notice this solution does not require a form. 请注意,此解决方案不需要表格。 You only need a form if your want to do bulk deletes with check boxes.
仅当您想要使用复选框进行批量删除时,才需要一个表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.