簡體   English   中英

登錄頁面問題(燒瓶,python)

[英]Login page issue (flask ,python)

我對 Python 和 Flask 非常陌生,正在嘗試創建具有用戶注冊和用戶登錄的網站。

我可以使用注冊頁面創建用戶,但我無法使用在用戶注冊期間使用的相同用戶憑據登錄。

下面是我寫的代碼

from flask import Flask, render_template, request, flash, redirect, url_for,session
import os
from flask_sqlalchemy import SQLAlchemy
from app import db
from werkzeug.security import generate_password_hash,check_password_hash

#from flaskr.db import get_db
#import pdb; pdb.set_trace()

app = Flask(__name__)
app.secret_key = os.urandom(24)


app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://support:support@rea-lnx-tdin01:5432/postgres'
db = SQLAlchemy(app)


@app.route('/', methods=["GET", "POST"])
def registration():
    Specialsym =['$','@','#']
    from models import name
    if request.method == "POST":
        username = request.form.get('Username')
        password = request.form.get('Password')
        conf_password = request.form.get('RepeatPassword')
        error = None
        if not username:
            error = 'Username is required'
            flash(error)
        elif not password:
            error = 'Password is required'
            flash(error)
        elif len(username)< 4:
            error = "User Name should be of minimum of 4 characters"
            flash(error)
        elif len(username)> 12:
            error = "User Name should be of maximum of 8 characters"
            flash(error)
        elif len(password)< 6:
            error = "Password should be of minimum of six characters"
            flash(error)
        elif len(password) > 8:
            error = "Password should be of maximum of eight characters"
            flash(error)
        elif not any(char.isupper() for char in password):
            error = "Password should have at least upper case letter"
            flash(error)
        elif not any(char.isdigit() for char in password):
            error = "Password should have at least one digit"
            flash(error)
        elif not any(char.islower() for char in password):
            error = "Password should have at least one lower case letter"
            flash(error)
        elif not any(char in Specialsym for char in password):
            error ="Password should have at least one of the symbols $ @ # "
            flash(error)
        elif name.query.filter_by(username=username).first() is not None:
            error = 'User  is already registered'
            flash(error)
        elif password != conf_password:
            error = 'Passwords Must Match'
           # pass_1 = password
            #pass_2 = request.form.get('RepeatPassword')
            flash(error)
            #flash(pass_1)
            #flash(pass_2)
        elif error is None:
            user = name(username=username, password=generate_password_hash(password))
            db.session.add(user)
            db.session.commit()
            flash(" User Created successfully ")

        #return redirect(url_for('login_page'))
    return render_template('Esp_Index.html')


@app.route('/loginpage/', methods=["GET", "POST"])
def login():
    return render_template('Loginpage.html')


@app.route('/Fulfilment/', methods=["GET", "POST"])
def fulfil():
   return render_template('Ful_Product_Provider.html')


@app.route('/login/', methods=["GET", "POST"])
def login_page():
    from models import name
    if request.method == 'POST':
        username = request.form.get('Username')
        password = request.form.get('Password')
        flash(username)
        error = None

        user = name.query.filter_by(username=username).first()
        if user is None:
            error = "User Name doesn't Exist or it's Invalid!!"
            flash(error)
            flash(user)
        elif not check_password_hash(user['password'], password):
            error = "Incorrect Password"
            flash(error)
        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('fulfil.html'))

            #flash(error)
    return render_template('Loginpage.html')
神社代碼####

ESP_Base.html

 <,DOCTYPE html> <html lang="en"> <head> {% block head %} <title>{% block title %}{% endblock %}</title> <meta name="viewport" content="width=device-width; initial-scale=1"> <meta http-equiv="Content-Type" content="text/html, charset=utf-8" /> <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar; 0), }; false). function hideURLbar(){ window,scrollTo(0;1), } </script > <.-- Custom Theme files --> <link href="{{url_for('static'.filename='css/Styles.css')}}" rel="stylesheet" type="text/css" media="all" /> <?-- //Custom Theme files --> <link href="//fonts:googleapis,com/css,family=Roboto,300,300i,400:400i.700.700i" rel="stylesheet"> <.-- //web font --> {% endblock %} </head> <body> <div id="content" >{% block content %} {% endblock %}</div> <ul class="colorlib-bubbles"> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3:5.1/jquery.min.js"></script> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </body> </html>
索引.html#########
 {% extends "ESP_Base.html" %} {% block title %}Enterprise App Support Console{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block body %} {% block content %} <h1>Enterprise Appsupport SignUp Portal Form</h1> {% with errors = get_flashed_messages() %} {% if errors %} {% for error in errors %} <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>{{ error }}</strong> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> {% endfor %} {% endif %} {% endwith %} <div class="main-w3layouts wrapper"> <div class="main-agileinfo"> <div class="agileits-top"> <form action="" method="POST"> <input type="text" name="Username" placeholder="Username" value="{{request.form.get.Username}}"> <input type="Email" name="Email" placeholder="Email" value="{{request.form.get.Email}}"> <input type="Password" name="Password" placeholder="Password" value="{{request.form.get.Password}}"> <input type="Password" name="RepeatPassword" placeholder="ConfirmPassword" value="{{request.form.get.RepeatPassword}}"> <input type="submit" value="SIGNUP"> </form> <p>Don't have an Account? <a href="{{ url_for('login') }} "> Login Now!</a></p> </div> </div> </div> {% endblock %} {% endblock %}

登錄頁面

{% extends "ESP_Base.html" %} {% block title %}Enterprise App Support Console{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block body %} {% block content %} <h1>Enterprise Appsuppor Login</h1> {% with errors = get_flashed_messages() %} {% if errors %} {% for error in errors %} <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>{{ error }}</strong> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> {% endfor %} {% endif %} {% endwith %} <div class="main-agileinfo"> <div class="agileits-top"> <form action="" method="POST"> <input type="text" name="Username" placeholder="Username" > <input type="password" name="Password" placeholder="Password"> <p><input type = "submit" value = "Login"/></p> </form> </div> </div> {% endblock %} {% endblock %}

你能告訴我可能是什么問題嗎?

提前致謝

問題出在Index.html中。 使用url_for('login_page')而不是url_for('login') ,如下所示。

<p>Don't have an Account? <a href="{{ url_for('login_page') }} "> Login Now!</a></p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM