yum
2024 年3 月 12 日 05:51
1
点击delete movie item会显示405 error,我看了下是因为POST之后紧接着又发送了一个GET request (就是说这个GET request本不应该被触发的。。。),但是怎么也想不通原因。在delete view function里加上GET method能暂时解决问题,但是没有解决根源。中间一边跟着tutorial做一边自己加了一些feature,但是已经做到这一步了不确定是哪一环节有问题,除了delete其他部分都表现挺正常。test跑过了没有问题。直接clone tutorial 从网页端尝试delete又是好的(注意到只发送了POST 请求并跳转到主页。)
@app.route('/movie/delete/<int:movie_id>', methods=['GET', 'POST']) # 限定只接受 POST 请求
# https://stackoverflow.com/questions/20689195/flask-error-method-not-allowed-the-method-is-not-allowed-for-the-requested-url
@login_required # 登录保护
def delete(movie_id):
if 'username' not in session:
flash("You were logged out due to inactivity. Please log in again.")
return redirect(url_for('index'))
movie = Movie.query.get_or_404(movie_id) # 获取电影记录
db.session.delete(movie) # 删除对应的记录
db.session.commit() # 提交数据库会话
flash('Item deleted.')
session.modified = True
return redirect(url_for('index')) # 重定向回主页
127.0.0.1 - - [...] "POST /movie/delete/1 HTTP/1.1" 302 -
127.0.0.1 - - [...] "GET /movie/delete/1 HTTP/1.1" 405 -
requirements.txt如下,基本上和tutorial重叠的包版本都是一样的:
alembic==1.13.1
Babel==2.14.0
blinker==1.7.0
Bootstrap-Flask==2.3.3
click==8.1.7
dnspython==2.4.2
dominate==2.9.1
email-validator==2.1.0.post1
Flask==2.1.3 # downgrade from 3.0.0
Flask-Login==0.6.3
Flask-Mail==0.9.1
Flask-Migrate==4.0.5
Flask-Principal==0.4.0
# Flask-Security-Too==5.3.3
Flask-SQLAlchemy==2.5.1 #downgrade from 3.1.1
Flask-WTF==1.2.1
idna==3.6
importlib-resources==6.1.1
itsdangerous==2.1.2
Jinja2==3.1.3
Mako==1.3.0
MarkupSafe==2.1.3
passlib==1.7.4
speaklater==1.3
SQLAlchemy==1.4.39 #downgrade from 2.0.25
typing_extensions==4.9.0
visitor==0.1.3
Werkzeug==2.1.2 # downgrade from 3.0.1
WTForms==3.1.2
python是3.9.12.
因为网上没有找到很多跟我一样的案例,有点无解。。。
有任何见解指点都非常感谢。
yum
2024 年3 月 12 日 08:38
3
代码部分:
base.html:
<!DOCTYPE html>
{% from 'bootstrap5/form.html' import render_form %}
<html lang="en">
<head>
{% block head %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
...
</title>
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
{% endblock %}
</head>
<body>
<!-- 插入到页面标题上方 -->
{% for message in get_flashed_messages() %}
<div class="alert">{{ message }}</div>
{% endfor %}
<h2>
<img alt="Avatar" class="avatar" src="{{ url_for('static', filename='images/avatar.png') }}">
...
</h2>
<nav>
<ul>
<li><a href="{{ url_for('index') }}">Home</a></li>
{% if current_user.is_authenticated %}
<li><a href="{{ url_for('settings') }}">Settings</a></li>
<li><a href="{{ url_for('logout') }}">Logout</a></li>
...
{% else %}
<li><a href="{{ url_for('login') }}">Login</a></li>
{% endif %}
</ul>
</nav>
{% block content %}{% endblock %}
<footer>
<small>© 2018 <a href="http://helloflask.com/book/3">HelloFlask</a></small>
</footer>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>
index.html:
{% extends 'base.html' %}
{% block content %}
<p>{{ movies|length }} Titles</p>
{% if current_user.is_authenticated %}
{{ render_form(form) }}
<ul class="movie-list">
{% for movie in movies %}
<li>{{ movie.title }} - {{ movie.year }}
<span class="float-right">
{% if current_user.is_authenticated %}
<a class="btn" href="{{ url_for('edit', movie_id=movie.id) }}">Edit</a>
...
<form class="inline-form" method="post" action="{{ url_for('delete', movie_id=movie.id) }}">
<input class="btn" type="submit" name="delete" value="Delete">
</form>
{% endif %}
<a class="imdb" href="https://www.imdb.com/find?q={{ movie.title }}" target="_blank" title="Find this movie on IMDb">IMDb</a>
</span>
</li>
{% endfor %}
</ul>
{% else %}
...
{% endif %}
<img alt="Walking Totoro" class="totoro" src="{{ url_for('static', filename='images/totoro.gif') }}" title="to~to~ro~">
{% endblock %}
script.js
...
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.inline-form').forEach(function(form) {
form.addEventListener('submit', function(event) {
event.preventDefault();
var confirmed = confirm('Are you sure?');
if (confirmed) {
// Submit the form if the user confirmed
console.log('Form submitted'); #注意到这个在console会很快闪一下又消失了
form.submit();
}
});
});
});
即使是一开始用onclick="return confirm('Are you sure?')"
好像也是报405.
基本上照着https://github.com/helloflask/watchlist来的,也翻看了网上相关的帖子,依然很不解。目前还算是入门,所以理解各方面不是很深,也许是有什么我明显忽略的地方或者角度。