如何在< td>中提供超链接标签为烧瓶删除方法 [英] how to give the hyperlink in <td> tag for flask deletion method

查看:197
本文介绍了如何在< td>中提供超链接标签为烧瓶删除方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何给出删除方法的超链接。这里是代码的HTML代码。我用HTML编写超链接。那么,如何通过瓶颈路径的URL。
我正在使用Flask框架,我试图从数据库中删除一个条目。下面的代码给出了这个错误:该方法不允许用于请求的URL。

  {%extendsbase.html %{
{%block head%}
{{super()}}
{%endblock%}
{%block navbar%}
{{super ()}}
{%endblock%}
{%block content%}
< div class =row>


在您的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,建议使用 fetch


HTML表单元素不能用于除POST或GET,所以对其他方法的支持应该通过javascript中的XMLHttpRequest来完成。




$ 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>


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."

{% 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'))

解决方案

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

<form action="{{ url_for('delete_user', postID=values.id) }}" method="POST">
</form>

view

@app.route('/delete/<int:post_id>', methods=['POST'])

Improved answer

If you want DELETE request to flask use Javascript, I suggest to use fetch

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><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>

这篇关于如何在&lt; td&gt;中提供超链接标签为烧瓶删除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆