一个简单的问题

2024-04-28 11:57:50 发布

您现在位置: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() 

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>

好的,me问题是错误405,它说:

*不允许使用方法

请求的URL不允许使用该方法。*

这发生在我走回家路线时(我是说“/”)

我得到的另一个错误代码是500,上面写着:

内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误

如果你发现了问题,我可能还有其他我不知道的问题,请告诉我,如果你知道解决方法,请解决它。谢谢:)


Tags: nameformappgetreturniftimerequest