完成注册功能

Posted 曹馨文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。

  1. js文件: onclick函数return True时才提交表单,return False时不提交表单。
  2. html文件:
    1. <form>中设置 action和method="post"
      from flask import Flask, render_template, request, redirect, url_for, session
      from flask_sqlalchemy import SQLAlchemy
      
      
      import config
      app = Flask(__name__)
      app.config.from_object(config)
      db = SQLAlchemy(app)
      
      
      class User(db.Model):
           __tablename__ = ‘user‘
           id = db.Column(db.Integer, primary_key=True, autoincrement=True)
           username = db.Column(db.String(20), nullable=False)
           password = db.Column(db.String(20), nullable=False)
           nickname = db.Column(db.String(50))
      
      
      
      @app.route(‘/‘)
      def index():
           return render_template(‘login.html‘)
      
      @aqq.route(‘/regist/‘, methods=[‘GET‘, ‘POST‘])
      def regist():
          if request.method ==‘GET‘
              return  render_template(‘login.html‘)
          else
              usern = request.form.get(‘username‘)
              passw = request.form.get(‘password‘)
              user = User.query.filter(User.username == usern).first()
              if user:
                  if user.passw == passw:
                      session[‘user‘] = usern
                      session.permanent = True
                      return redirect(url_for(‘base‘))
              else:
                  return u‘error usern or passw‘
      
                  #
                  # return render_template(‘login.html‘)
      
          @app.route(‘/regist/‘, methods=[‘GET‘, ‘POST‘])
          def regist():
              if request.method == ‘GET‘:
                  return render_template(‘.html‘)
              else:
                  username = request.form.get(‘username‘)
                  print(username)
                  password = request.form.get(‘password‘)
                  user = User.query.filter(User.username == username).first()
                  if user:
                      return ‘username existed‘
                  else:
                      user = User(username=username, password=password)
      
                      db.session.add(user)
                      db.session.commit()
                      return redirect(url_for(‘login‘))
      
          @app.route(‘/zimoban/‘)
          def zimoban():
              return render_template(‘.html‘)
      
          @app.route(‘/zzimoban/‘)
          def zzimoban():
              return render_template(‘.html‘)
      
          @app.route(‘/index/‘)
          def index():
              return render_template(‘.html‘)
      
          if __name__ == ‘__main__‘:
              app.run(debug=True)
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
      
      
      <form action="{{ url_for(‘denglu‘) }}" method="post">
          <div id="tittle"><h2 align="center">登录</h2></div>
          <div class="input_box">
              账户: <input id="uname" type="text" placeholder="请输入用户名" name="username">
          </div>
          <div class="input_box">
              密码: <input id="upass" type="password" placeholder="请输入密码" name="password">
          </div>
          <div id="error_box"><br></div>
          <div class="input_box">
              <button onclick="foLogin()">登录</button>
          </div>
      
          <button type="button" onclick="foLogin()"><a href="login.html">注册</a></button>
          </form>
      import os
      
      SECRET_KEY=os.urandom(24)
      
      SQLALCHEMY_DATABASE_URI=‘mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8‘
      SQLALCHEMY_TRACK_MODIFICATTONS = False

       

    2. <input> 中设置 name
  3. 主py文件中:
    1. from flask import  request, redirect, url_for
    2. @app.route(‘/regist/‘, methods=[‘GET‘, ‘POST’])

def regist():

   if request.method == ‘GET‘:

        return render_template(‘regist.html‘)

   else:

        username = request.form.get(‘username’)#获取form中的数据

        判断用户名是否存在:存在报错

  不存在,存到数据库中

       redirect重定向到登录页

以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章

解决方案电影标题中缺少代码的片段,完成挑战更多[关闭]

带有固定按钮的片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

完成注册功能