微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python flask简单问题

如何解决python flask简单问题

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() 

deadhead.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>

home.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模板中的参数“名称”引起的,而该参数未在render_template()中定义。但是尝试设置DEBUG = 1以获得更好的错误消息

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。