skip to Main Content

I am trying to make {{password}} change to a different password each time ‘submit’ is pressed. I am using Flask with html to do this. The function that creates the password is written in python.

I have tried making password an empty string in the Flask file expecting it to be empty each time ‘Submit’ is pressed. However, it did not change the password. I am trying to change {{password}} to a new {{password}} each time submit is pressed.

from flask import Blueprint,render_template, request, g
from gen import main


#This is where the different website pages will be implemented with python
views = Blueprint(__name__,'views')

@views.route('/')
def home():
    return render_template('home.html')

@views.route('/index.html')
def generator():
    return render_template('index.html', password = '')

@views.route("/result", methods=['POST', 'GET'])
def result():
    password = ''
    return render_template("result.html",password = password + main())

def main():
    pword =''
    while True:
        x = randint(randint(20,30),randint(30,40))
        randoml = []
        while len(password) < x:
            lettersL = letters(alphabet)
            numbersL = numbers(alphabet)
            symbolsL = symbols(alphabet)
            randoml.append((random.choice(lettersL)))
            randoml.append((random.choice(numbersL)))
            randoml.append((random.choice(symbolsL)))
            if len(randoml) > 10000: password.append(random.choice(randoml)) 
        pword = ''.join(password)
        return pword
<html>
    <head>Generated Password</head>

    <body>

        <form class = "password" action = "/result" method = "POST">
            <center>
                {% if password%}
                <h3 style="color: rgb(216, 44, 216)">Your password is {{password}}</h3>
                {% endif%}
            </center>
        </form>
        <form class = "grid" action = "/result">
            <center>
            <input type="Submit" class ="file_submit" value="Generate New Password">
                {% if password%}
                <h3 style="color: rgb(216, 44, 216)"></h3>
                {% endif%}
            </center>
        </form>


    </body>
</html>

2

Answers


  1. I don’t think you need an infinite while True loop on your main function.

    Here is how I would do it:

    from flask import Blueprint, render_template
    from gen import generate_password
    
    views = Blueprint(__name__, 'views')
    
    @views.route('/')
    def home():
        return render_template('home.html')
    
    @views.route('/index.html')
    def generator():
        return render_template('index.html', password='')
    
    @views.route("/result", methods=['POST', 'GET'])
    def result():
        password = generate_password()
        return render_template("result.html", password=password)
    
    def generate_password():
        x = randint(randint(20, 30), randint(30, 40))
        randoml = []
        while len(randoml) < x:
            lettersL = letters(alphabet)
            numbersL = numbers(alphabet)
            symbolsL = symbols(alphabet)
            randoml.append((random.choice(lettersL)))
            randoml.append((random.choice(numbersL)))
            randoml.append((random.choice(symbolsL)))
        password = ''.join(random.sample(randoml, x))
        return password
    
    Login or Signup to reply.
  2. Here is a much simpler and better performing mechanism for generating a password.

    import random
    
    letters = "abcdefghijklmnopqrstuvwxyz"
    numbers = "0123456789"
    symbols = '$_&%'
    
    def main():
        alphabet = list(letters + numbers + symbols)
        random.shuffle(alphabet)
        x = random.randint(20,40)
        return ''.join(alphabet[:x])
    
    print(main())
    print(main())
    print(main())
    

    Output:

    v3br$_dtcf4y5qzmg&ojw61n0%lk
    p2$307t1&h9dlezqj6oambyis%fv54n_rkxcw8
    wpdx8k_0ovr&saj76$z19hg5i2%ytn3e4lu
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search