SO中文参考
首页
(current)
程序语言
c
java
python
c++
go
javascript
swift
c#
操作系统
linux
ubuntu
centos
unix
数据库
oracle
mysql
mongodb
postgresql
框架
node.js
angular
react-native
avalon
django
twisted
hadoop
.net
移动开发
android
ios
搜索
在 jinja2 模板中创建 Flask 应用程序 url 的链接') def get_post(年、月、标题): # 我的代码 要显示 l...</desc> <question vote="45"> <p>在我的 Flask 应用程序中,我有一个显示帖子的视图</p> <pre><code>@post_blueprint.route('/post/<int:year>/<int:month>/<title>') def get_post(year,month,title): # My code </code></pre> <p>要显示最后 10 条条目,我有以下视图:</p> <pre><code>@post_blueprint.route('/posts/') def get_all_posts(): # My code return render_template('p.html',posts=posts) </code></pre> <p>现在,当我显示最后 10 篇帖子时,我想将帖子的标题转换为超链接。 目前我必须在我的 jinja 模板中执行以下操作才能实现此目的:</p> <pre><code><a href="/post/{{year}}/{{month}}/{{title}}">{{title}}</a> </code></pre> <p>有什么方法可以避免对 url 进行硬编码吗? </p> <p>像 <pre><code>url_for</code></pre> 函数一样,用于创建 Flask url,如下所示:</p> <pre><code>url_for('view_name',**arguments) </code></pre> <p>我尝试过寻找一个,但找不到。</p> </question> <answer tick="true" vote="88"> <p>我觉得你在这里问了两个问题,但我会尝试......</p> <p>对于发布网址,您可以这样做:</p> <pre><code><a href="{{ url_for('post_blueprint.get_post', year=year, month=month, title=title)}}"> {{ title }} </a> </code></pre> <p>要处理静态文件,我强烈建议使用像 <a href="http://flask-assets.readthedocs.org/en/latest/index.html" rel="nofollow noreferrer">Flask-Assets</a> 这样的资产管理器,但要使用普通烧瓶来做到这一点:</p> <pre><code>{{ url_for('static', filename='[filenameofstaticfile]') }} </code></pre> <p>如果您想了解更多信息,我强烈建议您阅读。 <a href="http://flask.pocoo.org/docs/quickstart/#static-files" rel="nofollow noreferrer">http://flask.pocoo.org/docs/quickstart/#static-files</a> 和 <a href="http://flask.pocoo.org/docs/quickstart/#url-building" rel="nofollow noreferrer">http://flask.pocoo.org/docs/quickstart/#url-building</a></p> <p><strong>编辑使用 kwargs:</strong></p> <p><em>只是以为我会更彻底......</em></p> <p>如果您想像这样使用<pre><code>url_for</code></pre>:</p> <pre><code>{{ url_for('post_blueprint.get_post', **post) }} </code></pre> <p>你必须改变你的观点,像这样:</p> <pre><code>@post_blueprint.route('/posts/') def get_all_posts(): models = database_call_to_fetch_posts() # This is assuming you use some kind of data-model posts = [] for model in models: posts.append(dict(year=model.year, month=model.month, title=model.title)) return render_template('p.html', posts=posts) def database_call_to_fetch_posts(): posts = [] # fetch posts here as a list of objects ... return posts </code></pre> <p>那么你的模板代码可以如下所示:</p> <pre><code>{% for post in posts %} <a href="{{ url_for('post_blueprint.get_post', **post) }}"> {{ post['title'] }} </a> {% endfor %} </code></pre> <p>此时,我实际上会在模型上创建一个方法,这样您就不必将其转换为字典,但走到这一步取决于您:-)。</p> </answer> </body></html>
问题描述
投票:0
回答:0
python
flask
jinja2
最新问题
浏览器在发送请求进行身份验证时不保存 cookie
如何在 ModalBottomSheet 中制作一个静态位置的按钮?
如何使用C#在AWS Lambda和API Gateway上部署Prince 13.5?
Power BI - 未来日期的余额
从 pdf 转换时如何获得更好的图像质量?
在 Python 中使用 pynput 的线程和热键问题(无限循环和响应性)
为什么此请求在 Insomnia/Postman 中有效,但在 Python 请求中无效?
无法使用 pip install pytorch-pretrained-bert
C++ 初始化具有 unique_ptr 的 std::vector
使用变量地址读取另一个进程的内存
阻止 Laravel 中未付费用户访问视频
nuxt 组件仅在生产中渲染两次
System.Reflection.TargetInvocationException:配置 AutoMapper 配置文件时,“表达式 'dest => dest' 必须解析为顶级成员”
MAUI 轮播无法实时更改项目、空白幻灯片、iOS 底部的灰色条
防止 API Gateway 接收对 robots.txt 文件的请求
该功能仅适用于包含打印语句的情况
如何在 Swift 中启用 URLSession.shared.data 中的 JavaScript
迁移到 Kotlin 2.0 导致 KSP 出现错误
绘制带有圆角的 SVG 多边形的架构,它可以缩放到不同的尺寸,但保持相同的圆角?
禁用在标题区域上滚动
© www.soinside.com 2019 - 2024. All rights reserved.