如何在html中删除python中的一个变量?(我用的是Flask)

eivgtgni  于 2023-06-20  发布在  Python
关注(0)|答案(2)|浏览(122)

每次按下“提交”时,我都尝试将{{password}}更改为不同的密码。我使用Flask和HTML来做这件事。创建密码的函数是用python编写的。
我尝试在Flask文件中将password设置为空字符串,希望每次按下“Submit”时它都是空的。但是,它没有更改密码。每次按下提交时,我都试图将{{password}}更改为新的{{password}}。

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>
41zrol4v

41zrol4v1#

下面是一个更简单、性能更好的生成密码的机制。

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())

输出:

v3br$_dtcf4y5qzmg&ojw61n0%lk
p2$307t1&h9dlezqj6oambyis%fv54n_rkxcw8
wpdx8k_0ovr&saj76$z19hg5i2%ytn3e4lu
qij5mzcb

qij5mzcb2#

我不认为你需要在你的main函数上使用一个无限的while True循环。
我会这样做:

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

相关问题