В def user (): function мне нужно получить сообщение в блоге для пользователя, который в данный момент вошел в переменную, чтобы я мог передать его в мой html файл с помощью команды render_template (). Честно говоря, это, наверное, что-то очень простое, но я просто не знал, как это правильно сформулировать, когда делал поиск в Google. Благодарю вас!
class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
username = db.Column(db.String(20))
password = db.Column(db.String(30))
blog = db.Column(db.String(500))
admin = db.Column(db.String(5))
def __init__(self, username, password, blog, admin):
self.username = username
self.password = password
self.blog = blog
self.admin = admin
db.create_all()
values = users.query.all()
@app.route("/create_account", methods=["POST", "GET"])
def create_account():
if request.method == "POST":
create_user = request.form["create_username"]
create_pass = request.form["create_password"]
create_admin = request.form["create_adminer"]
found_user = users.query.filter_by(username=create_user).first()
found_pass = users.query.filter_by(password=create_pass).first()
found_adminer = users.query.filter_by(admin=create_admin).first()
try:
if found_user.username and found_pass.password:
flash("Account already exists")
return render_template("login.html")
except:
if create_admin == "True":
usr = users(create_user, create_pass, 'Your Blog Will Go Here', create_admin)
db.session.add(usr)
db.session.commit()
flash('Account Created')
return redirect(url_for("login"))
else:
usr = users(create_user, create_pass, 'Your Blog Will Go Here', "")
db.session.add(usr)
db.session.commit()
flash('Account Created')
return redirect(url_for("login"))
else:
return render_template('create_account.html')
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
use = request.form["usern"]
pas = request.form["passw"]
found_username = users.query.filter_by(username=use, password=pas).first()
print(found_username.username)
try:
adm = request.form['adminer']
found_admin = users.query.filter_by(admin=adm).first()
if found_username.username == use and found_username.password == pas and adm == 'True':
session['username'] = use
session['password'] = pas
session['admin'] = adm
return redirect(url_for("user"))
else:
if found_username.username == use and found_username.password == pas:
session['username'] = use
session['password'] = pas
return redirect(url_for("user"))
except:
flash('Incorrect Login Information')
return render_template('login.html')
else:
return render_template('login.html')
@app.route("/user")
def user():
use = session['username']
pas = session['password']
values = users.query.all()
try:
while True:
for item in values:
print(item.blog)
found_user = users.query.filter_by(username=use, password=pas, blog=item.blog).first()
if found_user.username == use and found_user.password == pas and found_user.blog == item.blog:
return render_template("user.html", username=use, blogs=item.blog)
break
#I know there isn't and except statement, will add it once i get the basic code structure i need. Thank You!
@app.route("/logout")
def logout():
session.pop("username", None)
session.pop("password", None)
session.pop("admin", None)
flash("You have been logged out", 'warning')
return redirect(url_for('login'))
#return render_template('login.html')
if __name__ == "__main__":
db.create_all()
app.run(debug=True)