从文本文档中检索来自用户名的电子邮件时遇到问题

trouble retrieving an email from an username from a text document

所以我有这个网站,允许我从已注册该网站的用户那里获取电子邮件,所以当你注册时,它会在这样的文本文件中添加你的不和谐用户名和你的电子邮件

Discorduser#0182 Test@example.com
Discordusernumber_one#0182 Testboi@example.co.uk

但是,当我输入第一个选项时,它会从第一行检索电子邮件,但如果我是第二个用户,它似乎找不到它,这是网站的代码...代码是 bottle.py 脚本的一部分,但这是此页面的主要部分

@get('/get_confirm')
def confirm():
    return CONFIRM_PAGE

@post('/get_confirm')
def confirm():
    name = request.forms.get('name')
    for line in open('Confirmations.txt', 'r').readlines():
        login_info = line.replace('\n', '').split()
        if name == login_info[0]:
            return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + login_info[1] + '</h1>')
        else:
            return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + 'That user is not valid' + '</h1>')
CONFIRM_PAGE = '''
  <!DOCTYPE html>
  <html>
  <head>
    <link rel="icon" href="/static/icon.ico">
    <title>TylerR - get_confirms</title>
    <style>
        .content {
  background: white;
  padding: 15px;
  border-radius: 3px;
}    

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #333;
        }

        li {
            float: left;
        }

        li a, .dropbtn {
            display: inline-block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }

        li a:hover, .dropdown:hover .dropbtn {
            background-color: red;
        }

        li.dropdown {
            display: inline-block;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            z-index: 1;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
        }

        .dropdown-content a:hover {background-color: #f1f1f1}

        .dropdown:hover .dropdown-content {
            display: block;
        }
    </style>
  </head>
  <body style='background-size: cover; background-image: url(\"/static/WebisteBackground.png\"); background-attachment: fixed;'>
    <ul>
        <li><a href='/'>Home</a></li>
        <li><a href='/commands'>Commands</a></li>
        <li><a href='/youtube'>Youtube</a></li>
        <li><a href='/downloads'>Downloads</a></li>
        <a href='https://www.youtube.com/channel/UCTHnGh3DpDXMyuL03ove1tQ'>
            <img align='right' src='/static/youtubelogo.png' style="width:40px;height:40px;">
        </a>
        <a href='https://www.instagram.com/drumsnaps/'>
            <img align='right' src='/static/Insta.png' style="width:40px;height:40px;">
        </a>
    </ul>
    </div>
    <center>
    <h1 style="font-family:Cooper Black; font-size: 7em;"><b>Confirmations<b></h1>
    <br>
    <h1>Please enter an username to retrieve email</h1>
    <h3>When entering name, replace any spaces with _</h3>
    <form method='POST'  action='/get_confirm'>
        <h2>Discord Username:</h2>
        <input name='name' type='text' placeholder='User#0000'>
        <br>
        <input type='submit'>
        </form>
        <div class='content'>
            <h1 id='emailbox'></h1>
        </div>
  </center>
  </body>
  </html>
  '''

我正在使用 bottle.py

所以,如果有人能告诉我为什么会发生这种情况,那就太好了

在您的检查中立即 returns - 成功(如果用户在第一行)或不成功(如果用户不在第一行)。你想return没有成功,只有在你检查了所有行之后。

@post('/get_confirm')
def confirm():
    name = request.forms.get('name')
    with open('Confirmations.txt', 'r') as f:
        for line in f:
            user, email = line.strip().split()
            if name == user:
                return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + email + '</h1>')

     return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + 'That user is not valid' + '</h1>')

此外,最好使用像jinja2这样的模板引擎来构建HTML页面。