I am new to flask and I try to do a project that you do signup and if the admin press a button next to your name then the home page for the user will change. Here is the code of flask:
from flask import Flask,redirect,url_for,render_template,request,flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager,UserMixin,login_required,login_user,logout_user,current_user
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField
from wtforms.validators import InputRequired,Length
from werkzeug.security import generate_password_hash,check_password_hash
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'abc'
db = SQLAlchemy(app)
class Users(db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(200),unique=True,nullable=False)
password = db.Column(db.String(200),unique=True,nullable=False)
db.create_all()
class RegisterForm(FlaskForm):
name = StringField('Username',validators=[InputRequired(),Length(min=4,max=30,message='Name must be from 7 to 30')])
password = PasswordField('Password',validators=[InputRequired(),Length(min=4,max=30,message='Password must be from 7 to 30')])
users = []
@app.route("/",methods=['POST','GET'])
def home():
form = RegisterForm()
try:
if form.validate_on_submit():
user = Users(name=form.name.data,password=form.password.data)
db.session.add(user)
db.session.commit()
users.append(form.name.data)
return redirect('/home')
except:
return 'Name doest exist'
return render_template("register.html",form=form)
#You don't have now to log in to be admin
#For the Project only
a = 3
@app.route("/admin",methods=['POST','GET'])
def admin():
if request.method == 'POST':
global a
a = 2
return render_template("new3.html",mylist=users)
@app.route("/home")
def home1():
return render_template("new4.html",r=a)
if __name__ == '__main__':
app.run(debug=True)
And for the new3.html:
<!DOCTYPE html>
<html>
<body>
{%for i in mylist%}
<p>{{i}}</p>
<form action="" method="post">
<button name='button1'>Press it</button>
</form>
{%endfor%}
</body>
So how can I do that? I just want the change of home page to be only to the user that button pressed. I tried to do that by sending from email but I want to the change be on the home page.
The functionality you describe is built into HTML: The Ordered Llist. With some template code like:
<body>
<ol>
{% for b in bros %}
<li>{{b}}</li>
{% endfor %}
</ol>
<body>
Renders in the browser like:
You need not manually deal with the count in your template, and items will appear in the HTML list in the same order as the python list myList (as python lists are ordered by default).