简体   繁体   English

进阶:如何在 Jinja2 中使用 href

[英]Advanced: How to use href in Jinja2

I want to use href in the jinja2 template to send the user to the the specific result based on id to learn more about that particular result.我想在jinja2模板中使用href将用户发送到基于id的特定结果,以了解有关该特定结果的更多信息。 I created a route url : ('view_assessment_result', '/view_assessment_result/{id}') that renders all the details about the specific result based on id .我创建了一个route url : ('view_assessment_result', '/view_assessment_result/{id}')它根据id呈现有关特定结果的所有详细信息。

What I want to do:我想做的事:
When a user clicks on the ID number, they will be sent to the route view_assessment_result/{some_id} that will render the specifics of that page based on the id found in the iteration for a in assessment... .当用户点击ID号时,他们将被发送到路由view_assessment_result/{some_id} ,该路由将根据迭代中找到的id呈现该页面的细节, for a in assessment... I tried looking into cases, but others seem to be using Flask , which I am NOT using.我尝试调查案例,但其他人似乎在使用Flask ,而我没有使用。 The template code below is not accomplishing the task at hand.下面的模板代码没有完成手头的任务。

See code below:见下面的代码:

route config:路由配置:

    #Route convention: resource 'name' 'url dispath'
    config.add_route('view_assessment_result', '/view_assessment_result/{id}')

jinja template金贾模板

        <tr>
        {% for a in assessment_results %}
                <td><a href="{{ '/view_assessment_result' }}">{{ a.id }}</a></td>
            <td>{{ a.owner }}</td>
            <td>{{a.assessment }}</td>
        </tr>
        {% endfor %}

view configuration that renders specific result sent by href above:呈现上面href发送的特定结果的视图配置:

@view_config(route_name='view_assessment_result', request_method='GET', renderer='templates/analyst_view.jinja2')
def view_assessment_result(request):
    with transaction.manager:
        assessment_result_id = int(request.matchdict['id'])
        assessment_result = api.retrieve_assessment_result(assessment_result_id)
        if not assessment_result:
            raise HTTPNotFound()
    #more code

You need to add the id into your URL path.您需要将 id 添加到您的 URL 路径中。 One way to add the id (from the Python variable a.id to your URL is to use the % string formatting operator , like so:添加 id 的一种方法(从 Python 变量a.id到您的 URL 是使用%字符串格式化操作符,如下所示:

<a href="{{ '/view_assessment_result/%s'%a.id }}">{{ a.id }}</a>

Also, if your a.id might include special characters ( / , & , etc), you can escape them via the urlencode filter:此外,如果您的a.id可能包含特殊字符( /&等),您可以通过urlencode过滤器对它们进行转义:

<a href="{{ '/view_assessment_result/%s'%a.id|urlencode }}">{{ a.id }}</a>

You can use request.route_path to generate the URL.您可以使用request.route_path生成 URL。

<a href="{{ request.route_path('view_assessment_result', id=a.id) }}">{{ a.id }}</a>

Documentation here: http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.route_path此处的文档: http : //docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.route_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM