如何将烧瓶中的自定义变量发送到JS/HTML以渲染图像?

发布于 2025-02-13 09:04:30 字数 877 浏览 2 评论 0原文

这是我的基本Python代码,将可变帐户名称发送到JavaScript上,该代码将呈现图像。

@app.route('/<username>', methods=['GET', 'POST'])
def user_profile_name(username):
    # return f"welcome to profile page {username}"
    my_friends = database.GET_FRIENDS(connection, username)
    # print(send_string)
    return render_template(f"user_profile.html", friends=my_friends, account_name=username)

这是我的html,应该使用该名称到达该文件

<div>{{account_name}}</div>
<div id="profile picture">
    <h1>Profile Picture</h1>
    <img src="{{url_for('static', filename='#UserData/{{account_name}}/profile/profile_pic.jpg')}}\"  width='200' height='200' />

,如果我手动地将名称放在那里,它可以正常工作,有什么想法我如何将我的名字发送给我?

filename='#UserData/MY_NAME_HERE/profile/profile_pic.jpg')}}\"  width='200' height='200'

谢谢你的时间

Here is my basic python code to send the variable account name over to the javascript which will render the image.

@app.route('/<username>', methods=['GET', 'POST'])
def user_profile_name(username):
    # return f"welcome to profile page {username}"
    my_friends = database.GET_FRIENDS(connection, username)
    # print(send_string)
    return render_template(f"user_profile.html", friends=my_friends, account_name=username)

Here is my HTML that should go to the file with that name

<div>{{account_name}}</div>
<div id="profile picture">
    <h1>Profile Picture</h1>
    <img src="{{url_for('static', filename='#UserData/{{account_name}}/profile/profile_pic.jpg')}}\"  width='200' height='200' />

The image is there, and if I put the name in manually like so, it works, any idea how I can send my name over?

filename='#UserData/MY_NAME_HERE/profile/profile_pic.jpg')}}\"  width='200' height='200'

thanks for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2025-02-20 09:04:30
  1. 消毒
  2. 对jinja2变量的字符串中的变量进行
account_name = secure_filename(account_name)
<img src="{{url_for('static', filename='#UserData/' ~ account_name ~ '/profile/profile_pic.jpg')}"  width='200' height='200' />

(还确定您是否要在 userdata 之前都想要该标签,以及骆驼盒和蛇盒的混合物?更好地使用dash案例吗?对于HTML)。

  1. Sanitize the file name
  2. Concatenate your variable inside the string for the Jinja2 variable
account_name = secure_filename(account_name)
<img src="{{url_for('static', filename='#UserData/' ~ account_name ~ '/profile/profile_pic.jpg')}"  width='200' height='200' />

(Also, are you sure that you want that hashtag before UserData, and that mix of camel case and snake case? Better to use dash case for HTML. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文