如何为烧瓶删除方法提供超链接。这是代码HTML代码。我用HTML编写了超链接。那么,如何在烧瓶路线中传递url。 我正在使用Flask框架,我试图从数据库中删除一个条目。下面的代码给出了这个错误:"请求的URL不允许使用该方法。"
{% extends "base.html" %}
{% block head %}
{{super()}}
{% endblock %}
{% block navbar %}
{{super()}}
{% endblock %}
{% block content %}
<div class="row">
<ol class="breadcrumb">
<li><a href="#">
<em class="fa fa-home"></em> </a></li>
<li class="active">Users>View/Edit</li>
</ol>
</div><!--/.row-->
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Address
</th>
<th>
Aadharimage
</th>
<th>
Password
</th>
<th>
Locations
</th>
<th>
Dateofsub
</th>
<th>
Lastlogin
</th>
<th>
Control
</th>
<th>
Delete
</th>
</tr>
</thead>
{% for values in endusers %}
<tr>
<td>{{values.name}}</td>
<td>{{values.email}}</td>
<td>{{values.address}}</td>
<td> <img src="{{url_for('static', filename='assets/images /{{values.aadharimage}}')}}" width="24" height="24">{{values.aadharimage}}</td>`
<!-- <td><img src=" {{url_for('static', filename=' /assets/images/tms.png')}}" width="25" height="50"/></td>-->
<td>********</td>
<td>{{values.locations}}</td>
<td>{{values.dateofsub}}</td>
<td>{{values.lastlogin}}</td>
<td><a href="/Control/resetuserpass/{{values.id}}" class="btn btninfo">Reset Password</a></td>
<td><a href="{{ url_for('delete_user', postID=values.id) }}" class="btn btn-danger">Delete</a></td>
</tr>
{% endfor %}
</table>
<a href = "/page/new user"> <em class="fa fa-xl fa-plus-circle color-blue"></em> </a>
</div>
</div>
{% endblock %}
Here is the flask code this is correct ? I am trying for 2 days please solve this problem. While running thus code it showing 404 and 405 method.
@app.route('/delete/<int:postID>', methods=['DELETE'])
def delete_user(postID):
if not session.get('logged_in'):
abort(401)
sqlsession.execute('delete from values WHERE id = ?', [postID])
flash('Entry was deleted')
return redirect(url_for('pageedituser'))
答案 0 :(得分:5)
在您的HTML中,您构建了一个GET方法。将您的方法更改为GET将解决此问题,但这不是正确的答案。答案是使用HTML表单向烧瓶服务器发出POST请求。
HTML 的
<form action="{{ url_for('delete_user', postID=values.id) }}" method="POST">
</form>
视图
@app.route('/delete/<int:post_id>', methods=['POST'])
改进答案
如果你想要使用Javascript的DELETE请求,我建议使用fetch
HTML表单元素不能直接用于POST或GET之外的任何内容,因此任何对其他方法的支持都应该通过javascript中的XMLHttpRequest来完成。
即)
HTML 的
<td><a href="#" class="btn btn-danger" onclick="deletePost(23);">Delete</a></td>
<script type="text/javascript">
function deletePost(postId) {
return fetch('http://localhost:5000/delete' + '/' + postId, {
method: 'delete'
}).then(response =>
console.log(response)
);
}
</script>