Search code examples
pythonflaskflask-sqlalchemyflask-login

Can not login with Flask Login. Error: Method Not Allowed, The method is not allowed for the requested URL


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.


Solution

  • I removed the

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

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