使用Flask-WTForm和Postgresql数据库登录表单

问题描述 投票:0回答:1

先谢谢了!!我做了以下事情来创建登录名或登录表单:1)创建密钥2)使用LoginManager并通过了应用程序3)为login.html

创建了一条路线

当我访问本地主机上的登录页面时,出现“方法不允许页面”。

这里是代码:-

app.py

``` 

from flask import Flask, render_template, request, flash, redirect, url_for, session, logging
    from flask_login import LoginManager

    from config import Config

    app = Flask(__name__)
    app.config.from_object(Config)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost/learning'
    db = SQLAlchemy(app)
    # LoginManager
    login_manager = LoginManager(app)# Login Form class


@app.route('/login', methods=['GET,''POST'])
def login():

    if request.method == 'POST':
        # get form fields
        username = request.form['username']
        password_candidate = request.form['password']

        # getting user
        login_user = Flask_Users.query.filter_by(username).first()

        if login_user > 0:
            # Get stored hash
            password = login_user.password  # is this right way to get one field from login_user??
            # Compare passwords
            if sha256_crypt.verify(password_candidate, password):
                app.logger.info('PASSWORD MATCHED')

        else:
            app.logger.info('NO user')

    return render_template('login.html')```

login.html
```


 {% extends 'layout.html' %}

    {% block body %}
      <h1>Login</h1>
        {% include 'includes/_messages.html' %}

      <form action="" method="POST">
        <div class="form-group">
          <label>Username</label>
          <input type="text" name="username" class="form-control" value={{request.form.username}}>
        </div>
        <div class="form-group">
          <label>Password</label>
          <input type="password" name="password" class="form-control" value={{request.form.password}}>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    {% endblock %}

I followed what was in the documentation page. In the console, I am getting code 405 and error is the pic[enter image description here][1]


  [1]: https://i.stack.imgur.com/fkf0m.png

Any help is appreciated. I did go through relevant posts on the website as well, but could not find anything relatable
python postgresql flask flask-sqlalchemy wtforms
1个回答
0
投票
您的代码中有错字

methods=['GET,''POST']

您需要更正为

methods=['GET','POST']

© www.soinside.com 2019 - 2024. All rights reserved.