python - python烧瓶简单问题
问题描述
蟒蛇:
from flask import Flask,request,render_template,redirect,url_for,session
app=Flask(__name__)
@app.route("/")
def h():
if request.cookies.get("session"):
return render_template('home.html',user=request.cookies.get("user"),time=request.cookies.get("time"))
else:
return redirect(url_for("form"))
@app.route('/form',methods=['GET','POST'])
def form():
if request.method=='POST':
u=request.form.get('username')
p=request.form.get('password')
if u=="nbi" and p=="1383":
session[u]=p
zz=make_response(render_template("home.html",user=u,time=str(datetime.now())))
zz.set_cookie("user",u)
zz.set_cookie("time",str(datetime.now()))
return zz
else:
return render_template('deadhead.html',status=0)
else:
return render_template("deadhead.html")
@app.route('/logout')
def o():
try:
session.pop(request.cookies.get("user"))
return redirect(url_for("deadhead"))
except:
return redirect(url_for("deadhead"))
if __name__=="__main__":
app.run()
死头.html:
<body>
{% if name == 1 %}
<h1 style="color:green">welcome *_*</h1>
{% endif %}
{% if name == 0 %}
<h1 style="color:red"> error loging in "_ </h1>
{% endif %}
<form action="http://127.0.0.1:5000/" method='POST'>
<input type="text" name="username: ">
<input type="text" name="password: ">
<input type="submit" value="send">
</form>
</body>
</html>
主页.html:
<body>
<a href="http://127.0.0.1:5000/logout">logout</a>
<h1>welcome mon ami french</h1>
<h3>username : {{user}} </h3>
<h3>time login : {{time}}
</body>
</html>
好的,我的问题是错误 405,它说:
*方法不允许
请求的 URL 不允许该方法。*
当我去家乡路线时会发生这种情况(我的意思是“/”)
我得到的另一个错误代码是 500,它说:
内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。
如果你发现问题,我可能还有其他我不知道的问题,请告诉我,如果你知道解决方案,请解决它,谢谢:)
解决方案
我猜其中一个是由你的 deadhead.html 模板中的参数“name”引起的,该参数没有在你的 render_template() 中定义。但尝试设置 DEBUG=1 以获得更好的错误消息
推荐阅读
- android - Android non-square hit areas. Can it be done?
- r - 不返回 NA 值的左连接
- java - AndroidRuntime: java.lang.RuntimeException: Parcelable遇到IOException写入可序列化对象
- vue.js - 如何使用带有 img 标签的 v-slot?
- go - proto 请求中枚举的输入验证
- ios - 向 iOS 设备发送数据消息会导致 FirebaseMessagingException:请求包含无效参数
- python - 如何从 Seaborn lmplot() 中提取线性模型参数?
- node.js - Twilio - 创建一个对话服务或一个对话用户 node.js
- python - 在google colab中安装neural_renderer_pytorch时出错
- android - 从 GitHub 导入 Android 项目时如何避免使用多个版本的 Gradle 并且只使用一个单一版本的 Gradle?