无法使用url_for在Flask上生成POST请求 [英] Cannot Generate a POST Request on Flask using url_for
问题描述
我正在尝试创建一个按钮,允许用户通过向后面的视图发送发布请求来删除我的系统上的用户。
我的挑战是我无法获取表单的提交部分来触发POST请求,它始终生成GET,然后无法删除用户:
< a href ={{url_for('users.user_delete_admin',id = user.id)}}class =btn btn-danger btn-xs>删除< / a> ;
我曾尝试将method =post作为参数传递给url_for方法,但那不会't似乎不起作用
我的视图处理程序
@users_blueprint。 route('/ admin / delete-user /< int:id>',methods = [GET,POST])
@login_required
@admin_login_required
def user_delete_admin(id ):
user = User.query.get((id))
如果request.method ==POST:
user.delete()
db.session.commit ()
return redirect(url_for('users.users_list_admin'))
users = User.query.all()
return render_template('users-list-admin.html',users =用户,current_user = current_user)
和html表单:
< table class =table table-striped table-bordered>
< thead>
< tr>
< th> ID< / th>
< th>使用者名称< / th>
是Admin?< / th>
< th>< / th>
< th>< / th>
< / tr>
< / thead>
< tbody>
{%用户中的用户%}
< tr>
< td> {{user.id}}< / td>
< td> {{user.name}}< / td>
< td> {{user.admin}}< / td>
< td>
< a href ={{url_for('users.user_update_admin',id = user.id)}}class =btn btn-info btn-xs>编辑< / a>
< / td>
< td>
< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
使用简单的表单处理这个过程相对简单,但由于某些原因,我无法正常工作。
谢谢 解决方案
像这样处理它:
< form method =postaction ={{url_for('users.user_delete_admin',id = user.id)}} >
< button class =btn btn-danger btn-xstype =submit>删除< / button>
< / form>
I am attempting to create a button that allows for a user to delete a user on the my system by passing through a post request to the following view. My challenge is that I cannot get submission portion of the form to trigger a POST request, it always generates GET and then fails to delete the user :
<a href="{{ url_for('users.user_delete_admin', id=user.id) }}" class="btn btn-danger btn-xs" >Delete</a>
I have tried passing in method ="post" as a parameter to the url_for method, but that doesn't seem to work
My view handler
@users_blueprint.route('/admin/delete-user/<int:id>', methods=["GET","POST"])
@login_required
@admin_login_required
def user_delete_admin(id):
user = User.query.get((id))
if request.method =="POST":
user.delete()
db.session.commit()
return redirect(url_for('users.users_list_admin'))
users = User.query.all()
return render_template('users-list-admin.html', users=users, current_user=current_user)
And the html form:
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Is Admin ?</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.admin }}</td>
<td>
<a href="{{ url_for('users.user_update_admin', id=user.id) }}" class="btn btn-info btn-xs">Edit</a>
</td>
<td>
<a href="{{ url_for('users.user_delete_admin', id=user.id) }}" class="btn btn-danger btn-xs" >Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Handling this with a simple form is relatively simple, but for some reason I cannot get this to function properly.
Thanks
Handled it like this:
<form method = "post" action = "{{ url_for('users.user_delete_admin', id=user.id) }}">
<button class="btn btn-danger btn-xs" type="submit">Delete</button>
</form>
这篇关于无法使用url_for在Flask上生成POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!