[英]How to add the data in HTML page from Flask
@blueprint.route("/register/", methods=["GET", "POST"])
def register():
"""Register new user."""
form = RegisterForm(request.form)
print(form, form.username.data)
if form.validate_on_submit():
User.create(
username=form.username.data,
email=form.email.data,
password=form.password.data,
active=True,
)
flash("Thank you for registering. You can now log in.", "success")
return redirect(url_for("public.home"))
else:
flash_errors(form)
return render_template("public/register.html", form=form)
This is my registration code, I am facing a challenge where I have to add username to the UI page and this remains in page in all the different segments (on the website which I am building).这是我的注册码,我面临着一个挑战,我必须将用户名添加到 UI 页面,并且它保留在所有不同部分的页面中(在我正在构建的网站上)。
Since username is required everywhere, I have added the part where I need it, to the base.html
and importing/ loading this in all my other pages.由于到处都需要用户名,因此我将需要的部分添加到
base.html
并在所有其他页面中导入/加载它。
Currently, I'm not able to set up the part where the username from the database is taken and added to the HTML page.目前,我无法设置从数据库中获取用户名并将其添加到 HTML 页面的部分。
In this part on the HTML page, I want to import this在 HTML 页面的这一部分,我想导入这个
<div class="user-card user-card--skin-dark notification-item-padding-x" style="background-image: '{{ asset_url_for("media/misc/bg-1.jpg")}}'">
<div class="user-card__avatar">
<img alt="Pic" class="hidden" src="{{ asset_url_for('media/users/300_25.jpg') }}">
<span class="badge badge--lg badge--rounded badge--bold font-success">
{{user.username}}
</span>
</div>
<div class="user-card__name">
{{user.first_name}
</div>
</div>
But I am not able to add this.但我无法添加这个。 Can anyone help me with this?
谁能帮我这个?
ps I am not good with the front end technologies and have said almost 0 knowledge of javascript ps 我不擅长前端技术,几乎对 javascript 的了解为零
Check 'User Loader Function' function.检查“用户加载程序功能”功能。 A snap of code is given below :
下面给出了一个代码快照:
In models.py在models.py中
from app import login
@login.user_loader
def load_user(id):
return User.query.get(int(id))
In routes.py在routes.py
from flask_login import current_user, login_user
from app.models import User
@blueprint.route("/register/", methods=["GET", "POST"])
def register():
"""Register new user."""
form = RegisterForm(request.form)
print(form, form.username.data)
if form.validate_on_submit():
User.create(
username=form.username.data,
email=form.email.data,
password=form.password.data,
active=True,
)
flash("Thank you for registering. You can now log in.", "success")
return redirect(url_for("public.home"))
else:
flash_errors(form)
return render_template("public/register.html", form=form)
In html在 html 中
<div class="user-card user-card--skin-dark notification-item-padding-x" style="background-image: '{{ asset_url_for("media/misc/bg-1.jpg")}}'">
<div class="user-card__avatar">
<img alt="Pic" class="hidden" src="{{ asset_url_for('media/users/300_25.jpg') }}">
<span class="badge badge--lg badge--rounded badge--bold font-success">
{{current_user.username}}
</span>
</div>
<div class="user-card__name">
{{user.first_name}
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.