如何在< td>中提供超链接标签为烧瓶删除方法 [英] how to give the hyperlink in <td> tag for flask deletion method
问题描述
我正在使用Flask框架,我试图从数据库中删除一个条目。下面的代码给出了这个错误:该方法不允许用于请求的URL。
{%extendsbase.html %{
{%block head%}
{{super()}}
{%endblock%}
{%block navbar%}
{{super ()}}
{%endblock%}
{%block content%}
< div class =row>
< li>< a href =#>
< em class =fa fa-home>< / em> < / A>< /锂>
< li class =active>使用者>检视/编辑< / li>
< / ol>
< / div><! - /。row - >
< div class =row>
< div class =col-md-12>
< table class =table table-striped table-hover>
< thead>
< tr>
< th>
名称
< / th>
< th>
电子邮件
< / th>
< th>
地址
< / th>
< th>
Aadharimage
< / th>
< th>
密码
< / th>
< th>
地点
< / th>
< th>
Dateofsub
< / th>
< th>
Lastlogin
< / th>
< th>
控制
< / th>
< th>
删除
< / th>
< / tr>
< / thead>
{%用于最终用户的值%}
< 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 =24height =24> {{values.aadharimage }}< / td>`
<! - < td>< img src ={{url_for('static',filename ='/assets/images/tms.png')}} width =25height =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>重置密码< / a>< / td>
< td>< a href ={{url_for('delete_user',postID = values.id)}}class =btn btn-danger>删除< / 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%}
这是烧瓶代码,这是正确的?我正在尝试2天,请解决这个问题。运行这样的代码时,它显示404和405的方法。
$ b @ app.route('/ delete /< int:postID>',methods = ['DELETE'])
def delete_user(postID):
如果不是session.get('logged_in'):
abort(401)
sqlsession.execute('从值中删除id =?',[postID])
flash('Entry was deleted')
return redirect(url_for('pageedituser'))
在您的HTML中,您构建了一个GET方法。改变你的方法GET将解决这个问题,但这不是正确的答案。答案是使用HTML表单向POST服务器发出POST请求。
html
< form action ={{url_for('delete_user',postID = values.id)}}method =POST>
< / form>
view
@ app.route('/ delete /< int:post_id>',methods = ['POST'])
$ b 改进的答案 如果您希望DELETE请求使用Javascript,建议使用 HTML表单元素不能用于除POST或GET,所以对其他方法的支持应该通过javascript中的XMLHttpRequest来完成。 how to give the hyper link for flask deletion method. Here it is the code HTML code. I have written in HTML for hyperlink. So, how to give pass the url in flask route.
I'm working with a Flask framework, and I am trying to delete an entry from the database. The code below gives this error: "The method is not allowed for the requested URL."
In your HTML you've constructed a GET method. Change your methods to GET will solve this problem but that's not the correct answer. The answer is to use HTML form to make the POST request to your flask server. html view Improved answer If you want DELETE request to flask use Javascript, I suggest to use The HTML form element cannot be used for anything other than POST or GET directly, so any support for other methods should be done via XMLHttpRequest in javascript. i.e) html
这篇关于如何在< td>中提供超链接标签为烧瓶删除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
fetch
$ b $ p $ lt; code>< td>< a href = #class =btn btn-dangeronclick =deletePost(23);>删除< / a>< / td>
< script type =text / javascript>
function deletePost(postId){
return fetch('http:// localhost:5000 / delete'+'/'+ postId,{
method:'delete'
} ).then(response =>
console.log(response)
);
}
< / script>
{% 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'))
<form action="{{ url_for('delete_user', postID=values.id) }}" method="POST">
</form>
@app.route('/delete/<int:post_id>', methods=['POST'])
fetch
<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>