网页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主界面</title>
<link rel="stylesheet" href="static\css\水波.css">
<!--<link rel="shortcut icon" href="static\mario.ico" type="image/x-icon">-->
</head>
<body background="static\images\bg2.png" style="background-repeat:no-repeat ; background-size:100% 100% ; background-attachment:fixed">
<div>
<a href="/shezhi" style="text-align:right">
<img src="static\images\hb2.png" alt="图片丢失" title="设置" style="height:150px ; weight:150px">
</a>
</div>
</body>
</html>
Python代码
from flask import Flask, render_template, request, redirect
import random as a1
app = Flask(__name__)
#获取文本自定义行
def hang(a):
f = open("txt/数据.txt", "r")
r = f.readlines()
b = print(r[a]) # 读第一行
return b
# 修改文本自定义行
def changetext(a, b):
with open('txt/数据.txt', 'r') as f:
lines = [] # 创建了一个空列表,里面没有元素
for line in f.readlines():
if line != '\n':
lines.append(line)
f.close()
with open('txt/数据.txt', 'w') as f:
for line in lines:
if a in line:
line = b
f.write('%s\n' % line)
else:
f.write('%s' % line)
# 定义开始界面
@app.route('/')
def main():
print(hang(0))
if hang(0) == 'white' or hang(0) == 'None':
return render_template('主页面.html',bg='bg')
elif hang(0) == 'yello':
return render_template('主页面.html', bg='bg2')
@app.route('/shezhi')
def s1():
return render_template('设置.html')
@app.route('/shezhicg')
def s2():
bgc = request.args['bg']
print(bgc)
b1 = hang(0)
changetext(b1,bgc)
return redirect('/')
if __name__ == '__main__':
app.run()
[2022-01-12 12:46:55,901] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1968, in finalize_request
response = self.make_response(rv)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2098, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [12/Jan/2022 12:46:55] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [12/Jan/2022 12:46:56] "GET /favicon.ico HTTP/1.1" 404 -
文件路径
具体什么问题,你这个时 python报错吧
TypeError:view函数未返回有效响应。函数返回None或结束时没有返回语句。
要么加 else 把 if 语句补齐,要么在函数最后 return 点什么