flask注册登录

app.py
@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
        
        if User.query.filter_by(username=username).first():
           flash('该用户名已经被注册')
           return render_template('register.html')
        user = User(username=username, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('您的账号已创建,现在可以登录了!', 'success')
        return redirect(url_for('login'))
    return render_template('register.html') 
@app.route('/login')
def login():
    return render_template('login.html')

 

register.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
	<style type="text/css">
		.alert{
          color:red;
		  font-size:12.7px;
		
		}
	
	
	</style>
</head>
<body>
    <h1>注册</h1>
    <form method="POST" action="{{ url_for('register') }}">
	    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" required>
        <br>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
        <br>
        <button type="submit">注册</button>
    </form>
	<p>
	{% for message in get_flashed_messages() %}
        <div class="alert">{{ message }}</div>
    {% endfor %}
	</p>
</body>
</html>

login.html

<!DOCTYPE html>
<html>

<head>
  <title>登录界面</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
    }

   .container {
      width: 300px;
      margin: 0 auto;
      padding: 20px;
      background-color: #fff;
      border: 1px solid #ddd;
      border-radius: 5px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    h2 {
      text-align: center;
    }

    label {
      display: block;
      margin-bottom: 5px;
    }

    input[type="text"],
    input[type="password"] {
      width: 100%;
      padding: 10px;
      margin-bottom: 15px;
      border: 1px solid #ddd;
      border-radius: 3px;
    }

    button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: #fff;
      border: none;
      border-radius: 3px;
      cursor: pointer;
    }

    button:hover {
      background-color: #45a049;
    }

    a {
      display: block;
      text-align: center;
      margin-top: 10px;
      color: #4CAF50;
      text-decoration: none;
    }
  </style>
</head>

<body>
  <div class="container">
    <h2>登录</h2>
    <form>
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username">
      <label for="password">密码:</label>
      <input type="password" id="password" name="password">
      <button type="submit">登录</button>
      <a href="#">忘记密码?</a>
    </form>
  </div>
</body>

</html>

  

  

  

posted @ 2024-10-18 10:44  tec2019  阅读(10)  评论(0编辑  收藏  举报