2017-12-11 5 views
1

플라스크 삭제 방법에 대한 하이퍼 링크를 제공하는 방법. 여기 코드 HTML 코드입니다. 하이퍼 링크 용으로 HTML로 작성했습니다. 그래서,주는 방법 플라스크 경로에 URL을 전달합니다. Flask 프레임 워크로 작업 중이며 데이터베이스에서 항목을 삭제하려고합니다. 아래 코드는 "이 메서드는 요청 된 URL에 허용되지 않습니다."라는 오류 메시지를 표시합니다.하이퍼 링크를 제공하는 방법 <td> 플라스크 삭제 방법에 대한 태그

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

브라우저는 DELETE 방법을 전송하지 않습니다. 그러나 어떤 경우에도 링크는 오직 GET이 될 것이고 ** 당신은 GET에서 파괴적인 조작을해서는 안됩니다 **. 버튼이있는 양식을 사용해야합니다. –

+0

당신은 @daniel에 대한 간단한 정보를 줄 수 있습니까? –

답변

4

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']) 

개선 대답 당신이 사용하는 자바 스크립트를 플라스크에 요청을 삭제하려면, 내가 사용하는 것이 좋습니다

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.

즉)

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> 
관련 문제