Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据数据动态创建路由_Python_Python 3.x_Http_Flask_Routes - Fatal编程技术网

Python 根据数据动态创建路由

Python 根据数据动态创建路由,python,python-3.x,http,flask,routes,Python,Python 3.x,Http,Flask,Routes,是否可以根据数据库中的数据生成路由?目前我有一条总路线: @main.route('/profile’) def profile(user_name): return render_template('profile.html', name=user_name) 及 其中profile.html如下所示: {% extends "base.html" %} {% block content %} <div class="container has-text-centered"

是否可以根据数据库中的数据生成路由?目前我有一条总路线:

@main.route('/profile’)
def profile(user_name):

    return render_template('profile.html', name=user_name)

其中profile.html如下所示:

{% extends "base.html" %}

{% block content %}
<div class="container has-text-centered">
  <h1 class="title">
    Welcome, {{ name }}!
  </h1>
  <h3 class="title">
    This is a stream from your camera
  </h3>
  <img src="{{ url_for('main.video_stream') }}">
</div>
{% endblock %}
/profile/Test/stream?id=1
我想把我的路线改成这样就行了:

@main.route('/profile/<string:user_name>')
def profile(user_name):
@main.route(“/profile/”)
def配置文件(用户名):

@main.route('/video\u stream/'))
def视频流(流id):
但据我所知。。不知道如何正确解释…不会有其他路由,因为这只会创建一条路由。例如: 作为id为1的用户测试登录,我将有一个带有route/profile/Test?id=1的页面,所有内容都将正常工作。但是当我以不同的帐户登录到不同的机器上时,比如说id为2的Test2,那么之前的路由将停止存在,新的/profile/Test2?id=2将是这里唯一的路由


因此,可以动态创建新路由,但保持先前创建的路由处于活动状态?

因此,如果我理解正确,您只希望登录用户访问
/profile/Test?id=user\u id
?这不是一个真正的路由问题,更像是一个身份验证问题。您想要在个人资料页面中显示的内容可能如下所示:

{% extends "base.html" %}

{% block content %}
<div class="container has-text-centered">
  <h1 class="title">
    Welcome, {{ name }}!
  </h1>
  <h3 class="title">
    This is a stream from your camera
  </h3>
  <img src="{{ url_for('main.video_stream') }}">
</div>
{% endblock %}
/profile/Test/stream?id=1
来自flask导入请求,会话
@main.route(“/profile/”)
if request.args.get('id')==session.get('user\u id'))
通过#继续
在上面的示例中,我假设当用户成功登录到您的应用程序时,您将使用用户id设置名为user_id的会话。这样,用户将只能查看?id=与用户自己的id匹配的配置文件