直到最近,一切都很好,但现在我的网站不断给出“SESSION_COOKIE_NAME”和“NoneType”对象没有属性“修改”错误。但令人感兴趣的是,如果我更改代码,错误如何消失一段时间并不重要。请有识之士帮忙解答一下
以下是代码中最重要的部分:
import os
import plotly.express as px
import cs50
import pandas as pd
import json
import plotly
import requests
from dotenv import load_dotenv
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session, jsonify, url_for
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.security import check_password_hash, generate_password_hash
from datetime import datetime
from plotly.graph_objs import Figure, Scatter
from helpers import apology, login_required, lookup, usd
# Configure application
app = Flask(__name__)
load_dotenv()
# Configure session to use filesystem (instead of signed cookies)
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_USE_SIGNER"] = True
app.config["SECRET_KEY"] = os.urandom(24)
app.config["SESSION_COOKIE_NAME"] = 'session_cookie_name'
Session(app)
# I'm also using part of code like this in my app
session["user_id"] = rows[0]["id"]
# or
user_id = session["user_id"]
if __name__ == '__main__':
app.run
这里是错误日志:
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2527, in wsgi_app
ctx.push()
File "/usr/local/lib/python3.11/site-packages/flask/ctx.py", line 375, in push
self.session = session_interface.open_session(self.app, self.request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/flask_session/sessions.py", line 329, in open_session
sid = request.cookies.get(app.session_cookie_name)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 287, in session_cookie_name
warnings.warn(
DeprecationWarning: 'session_cookie_name' is deprecated and will be removed in Flask 2.3. Use 'SESSION_COOKIE_NAME' in 'app.config' instead.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1847, in finalize_request
response = self.process_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2343, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "/usr/local/lib/python3.11/site-packages/flask_session/sessions.py", line 353, in save_session
if session.modified:
^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'modified'
我只是没有尝试完全删除它并更改名称。登录帐户一段时间后会出现此问题,当尝试搜索该网站时,它只是给出此错误而不是 POST 请求。
您的输出由两部分组成,第一部分是警告。 而不是
sid = request.cookies.get(app.session_cookie_name)
修改为
sid = request.cookies.get(app.config["SESSION_COOKIE_NAME"] )
和
if session.modified:
会话似乎未初始化或没有.modified密钥。