Advertisement
Guest User

Untitled

a guest
Jun 18th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. from flask import Flask, render_template, request, redirect, url_for
  2. from flask_sqlalchemy import SQLAlchemy
  3. from datetime import datetime
  4.  
  5. application = Flask(__name__)
  6. application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
  7. application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  8. db = SQLAlchemy(application)
  9.  
  10.  
  11. class Users(db.Model):
  12.     id = db.Column(db.Integer, primary_key=True)
  13.     username = db.Column(db.String(32), primary_key=True, nullable=False)
  14.     email = db.Column(db.String(64), primary_key=True, nullable=False)
  15.     pword = db.Column(db.String(32), nullable=False)
  16.     date = db.Column(db.DateTime, default=datetime.utcnow)
  17.  
  18.     def __repr__(self):
  19.         return '<Users %r' % self.id
  20.  
  21.  
  22. @application.route('/')
  23. @application.route('/index.html')
  24. def hello():
  25.     return render_template("index.html")
  26.  
  27.  
  28. @application.route('/about/')
  29. @application.route('/about/index.html')
  30. def about():
  31.     return render_template("about/index.html")
  32.  
  33.  
  34. @application.route('/login', methods=["GET", "POST"])
  35. @application.route('/login/index.html', methods=["GET", "POST"])
  36. def login():
  37.     if request.method == "GET":
  38.         return render_template("login/index.html")
  39.     else:
  40.         username = request.form['username']
  41.         email = request.form['email']
  42.         pword = request.form['pword']
  43.  
  44.         user = Users(username=username, email=email, pword=pword)
  45.  
  46.         try:
  47.             db.session.add(user)
  48.             db.session.commit()
  49.             return "Вы успешно зарегистрировались"
  50.         except:
  51.             return "При регистрации произошла ошибка"
  52.  
  53.  
  54. if __name__ == "__main__":
  55.     application.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement