无法使用Flask登录。错误:方法不允许,请求的URL不允许使用该方法。

huangapple go评论74阅读模式
英文:

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(&#39;/login&#39;,methods=[&#39;GET&#39;,&#39;POST&#39;])
def login():
    if request.method == &#39;POST&#39;:
        name=request.form.get(&#39;email&#39;)
        print(name)
        user = db.session.query(User).filter_by(email=request.form.get(&#39;email&#39;)).first() 
        if user:
            if True:
            #if check_password_hash(request.form.get(&#39;password&#39;), user.password):
                login_user(user)
                return redirect(url_for(&#39;secrets&#39;,name=name))
    return render_template(&quot;login.html&quot;)

here is the login.html

{% extends &quot;base.html&quot; %}
{% block content %}

&lt;div class=&quot;box&quot;&gt;
    &lt;h1&gt;Login&lt;/h1&gt;

    &lt;form action=&quot;{{ url_for(&#39;secrets&#39;) }}&quot; method=&quot;POST&quot;&gt;
        &lt;input type=&quot;text&quot; name=&quot;email&quot; placeholder=&quot;Email&quot; required=&quot;required&quot;/&gt;
        &lt;input type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;Password&quot; required=&quot;required&quot;/&gt;
        &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-block btn-large&quot;&gt;Let me in.&lt;/button&gt;
    &lt;/form&gt;
&lt;/div&gt;

{% endblock %}

and here is secrets route and secrets.html which is user can access when it is logged in.

@app.route(&#39;/secrets&#39;,methods=[&#39;GET&#39;,&#39;POST&#39;])
@login_required
#bu secretsa girmek icin kural koydu!
def secrets():
    name = request.form.get(&#39;name&#39;)
    return render_template(&quot;secrets.html&quot;,name=name)

secrets.html

{% extends &quot;base.html&quot; %}
{% block content %}

&lt;div class=&quot;container&quot;&gt;
  &lt;h1 class=&quot;title&quot;&gt;Welcome, {{name}}&lt;/h1&gt;
  &lt;a href=&quot;{{url_for(&#39;download&#39;)}}&quot;&gt;Download Your File&lt;/a&gt;
&lt;/div&gt;
&lt;a href={{url_for(&#39;logout&#39;)}} &gt;logout the user&lt;/a&gt;
{% 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 == &#39;POST&#39;:. 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(&#39;/secrets&#39;,methods=[&#39;POST&#39;])

答案2

得分: 0

我从登录页面的HTML中删除了以下部分,并且它正常工作了:

action="{{ url_for('secrets') }}"

我仍然不知道为什么。

英文:

I removed the

action=&quot;{{ url_for(&#39;secrets&#39;) }}&quot;

part from login html and, it worked fine.
I still do not know how.

huangapple
  • 本文由 发表于 2023年7月18日 16:19:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定