【问题标题】:Create link with an id : href ="{{ url_for('users/%s'%user[0]) }}" /pb with endpoint and build url (jinja/python/flask )使用 id 创建链接: href ="{{ url_for('users/%s'%user[0]) }}" /pb 使用端点并构建 url (jinja/python/flask)
【发布时间】:2020-08-26 00:07:20
【问题描述】:

我正在开发一个包含 2 个应用程序(使用 python/flask 构建)的项目。 第一个应用程序。 py 是服务器 第二个client.py是客户端。

现在我正在建立从用户访问用户/8 的链接 但它不起作用

在 users.html / client.py @route /eror 来自 jinja 的消息下

users.html:

{%block body%}   

        <div class="users">
                <div class="list-group">
                        {% for user in users %}

                        <a href ="{{ url_for('users/%s'%user[0]) }}" class="list-group-item list-group-item-action">
                                <div class="d-flex w-100 justify-content-between">
                                        <h5 class="mb-1">{{user[1]}}</h5>
                                        <small>{{user[0]}}</small>
                                </div>
                                <p class="mb-1">{{user[2]}}</p>
                                <small>Know more...</small>
                        </a>

                        {% endfor %}
                </div>


        </div>

{%endblock%}

client.py:

  @client.route('/users/<id>')#ok works 
 def user(id=None):
     if 'logged_in' in session:
         r = actualUser.get(id)
         user=json.loads(r.text)
         return render_template('user.html', user=user)
     else:
         return redirect(url_for('login'))

错误信息:

 File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/users.html", line 1, in top-level template code
    {%extends "base.html" %}
    File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/base.html", line 44, in top-level template code
    {%block body%}
    File "/home/elodieb/Rendu/Python/Flask/flask_d02/ex_03/Client/templates/users.html", line 12, in block "body"
    <a href ="{{ url_for('users/%s'%user[0]) }}" class="list-group-item list-group-item-action">    
    werkzeug.routing.BuildError: Could not build url for endpoint 'users/7'. Did you mean 'users' instead?

【问题讨论】:

    标签: url jinja2 href endpoint


    【解决方案1】:

    看起来users.html 正在寻找users 列表(请参阅{% for user in users %}),而您正在传递一个user 实例(render_template('user.html', user=user))。

    您可以修改模板而不使用for in 循环,或者像render_template('user.html', users=[user]) 那样更新对render_template 的调用

    【讨论】:

      【解决方案2】:

      这里的错误信息非常明确。问题是在users.html 模板中对url_for 函数的无效使用。你写了这个

      url_for('users/%s'%user[0])
      

      url_for 需要一个“端点”参数(相应路由的名称,这里是'users')和作为关键字传递的变量论据。更多信息和使用示例在这里:https://flask.palletsprojects.com/en/1.1.x/quickstart/#url-building

      因此,您应该将模板指令替换为

      url_for('users', id=user[0])
      

      在修复此问题后,您可能最终会遇到其他错误,因为您将单个 user=user 变量传递给您的模板,而您想遍历 users 集合。

      【讨论】:

        猜你喜欢
        • 2021-03-17
        • 2011-11-20
        • 1970-01-01
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多