英文:
Can not login with Flask Login. Error: Method Not Allowed, The method is not allowed for the requested URL
问题
以下是您要翻译的内容:
这是登录路由。
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
name = request.form.get('email')
print(name)
user = db.session.query(User).filter_by(email=request.form.get('email')).first()
if user:
if True:
#if check_password_hash(request.form.get('password'), user.password):
login_user(user)
return redirect(url_for('secrets', name=name))
return render_template("login.html")
这是login.html
{% extends "base.html" %}
{% block content %}
<div class="box">
<h1>Login</h1>
<form action="{{ url_for('secrets') }}" method="POST">
<input type="text" name="email" placeholder="Email" required="required"/>
<input type="password" name="password" placeholder="Password" required="required"/>
<button type="submit" class="btn btn-primary btn-block btn-large">Let me in.</button>
</form>
</div>
{% endblock %}
这是secrets路由和secrets.html,用户可以在登录后访问。
@app.route('/secrets', methods=['GET', 'POST'])
@login_required
#bu secretsa girmek icin kural koydu!
def secrets():
name = request.form.get('name')
return render_template("secrets.html", name=name)
secrets.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1 class="title">Welcome, {{name}}</h1>
<a href="{{url_for('download')}}">Download Your File</a>
</div>
<a href={{url_for('logout')}} >logout the user</a>
{% endblock %}
您想要基本上登录用户,并访问secrets页面。它跳转到http://127.0.0.1:5000/secrets这个URL,但是报错405 Method is not allowed.
顺便说一下,我是一个新手。如果您想获取有关代码的更多信息,或者如果我没有提供足够的信息,请告诉我,我会尽力提供帮助。谢谢!
英文:
Here is the login route.
@app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'POST':
name=request.form.get('email')
print(name)
user = db.session.query(User).filter_by(email=request.form.get('email')).first()
if user:
if True:
#if check_password_hash(request.form.get('password'), user.password):
login_user(user)
return redirect(url_for('secrets',name=name))
return render_template("login.html")
here is the login.html
{% extends "base.html" %}
{% block content %}
<div class="box">
<h1>Login</h1>
<form action="{{ url_for('secrets') }}" method="POST">
<input type="text" name="email" placeholder="Email" required="required"/>
<input type="password" name="password" placeholder="Password" required="required"/>
<button type="submit" class="btn btn-primary btn-block btn-large">Let me in.</button>
</form>
</div>
{% endblock %}
and here is secrets route and secrets.html which is user can access when it is logged in.
@app.route('/secrets',methods=['GET','POST'])
@login_required
#bu secretsa girmek icin kural koydu!
def secrets():
name = request.form.get('name')
return render_template("secrets.html",name=name)
secrets.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1 class="title">Welcome, {{name}}</h1>
<a href="{{url_for('download')}}">Download Your File</a>
</div>
<a href={{url_for('logout')}} >logout the user</a>
{% endblock %}
I want to basically log in the user and, access it to secrets page. It goes to http://127.0.0.1:5000/secrets ths url but gives an 405 Method is not allowed error.
BTW i am really new. If you want further information about code or if i did not give enough information just tell me i will do my best. Thank you already.
答案1
得分: 0
Your code for /secrets
says it supports both GET
and POST
. Because you're supporting more than 1 method, you have to explicitly call out which code handles which method i.e. you have to do something like if request.method == 'POST':
. This would be similar to what you have for your /login
route.
The other option is to keep your code as is but change the route to only handle POST i.e. change to @app.route('/secrets', methods=['POST'])
英文:
Your code for /secrets
says it supports both GET
and POST
. Because you're supporting more than 1 method, you have to explicitly call out which code handles which method i.e. you have to do something like if request.method == 'POST':
. This would be similar to what you have for your /login
route.
The other option is to keep your code as is but change the route to only handle POST i.e. change to @app.route('/secrets',methods=['POST'])
答案2
得分: 0
我从登录页面的HTML中删除了以下部分,并且它正常工作了:
action="{{ url_for('secrets') }}"
我仍然不知道为什么。
英文:
I removed the
action="{{ url_for('secrets') }}"
part from login html and, it worked fine.
I still do not know how.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论