简体   繁体   English

如何在python金字塔中使用动态模板

[英]how to use dynamic templates in python pyramid

i've finished developing a website, it's working fine however i am trying to optimize my website by adding dynamic templates, and want to make sure that if it can be done on pyramid python. 我已经完成了网站的开发,但工作正常,但是我试图通过添加动态模板来优化我的网站,并希望确保是否可以在金字塔python上完成。

for example, in my jinja template i have the following: 例如,在我的jinja模板中,我有以下内容:

{% block article_detail %}
    <form action="{{request.route_url('Sports_News_Action',action=action)}}" method="post" class="form">
        {% if action =='edit' %}
            {{ form.id() }}

example in my controller: 我的控制器中的示例:

@view_config(route_name='Sports_News_Action', match_param='action=create',
             renderer='StarAdmin:templates/edit_sports.jinja2')
def general_create(request):
    entry = SportNews()

the request route will have to match the one in my controller in order to run the function. 请求路由必须与我的控制器中的路由匹配才能运行该功能。 what i want to do is how do i replace the one in jinja with a dynamic variable, to use the one jinja template lets say for different views/controllers with different route_names. 我想要做的是如何用动态变量替换jinja中的那个,使用一个jinja模板让我们说具有不同route_names的不同视图/控制器。

I think in your situation the simplest solution is to leave action undefined and the browser will submit the request to the current url. 我认为在您的情况下,最简单的解决方案是保留未定义的action ,浏览器会将请求提交到当前网址。 You only need to specify action if you want to submit the form to a different url than the current. 如果要将表单提交到与当前URL不同的URL,则仅需要指定action That being said, you can use lots of different options in pyramid to generate a url as well. 话虽如此,您也可以在金字塔中使用许多不同的选项来生成URL。 For example, request.url is the current url, or request.matched_route.name is the name of the current matched route. 例如, request.url是当前URL,或者request.matched_route.name是当前匹配的路由的名称。

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

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