问题标签 [templatetag]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
3315 浏览

django - django模板 - 在字符串变量中解析变量

我正在将动态内容(从数据库)拉到模板中。您可以将其视为一些简单的 CMS 系统。内容字符串包含一个模板变量。像这个(简化案例):

然后在模板中:

输出明显:
当前城市是:{{current_city}}
预期:
当前城市是:伦敦

我的问题 - 有没有办法在另一个变量中呈现变量名?使用自定义模板标签/过滤器似乎是一个好主意,但我试图创建一个没有成功......有什么想法可以解决这个问题吗?

0 投票
2 回答
443 浏览

django - Django:将 item.id 列为字段属性?

在我的模板中,我有:

is_solution 只是用户可以设置的复选框的布尔字段。

我面临的问题是,除了 {{mark_solved_form.is_solution}} 之外,所有表单都使用 id 正确填充,例如 id="marked_solved_form3"。我想我可以去模型表单并编辑属性:

到目前为止,一切都很好。除了表单加载时,我为每个列表表单中的每个复选框得到以下信息:

就好像 django 忽略了 modelform 正在一起转义模板标签 {{link.id}} 。理想情况下,它会为每个表单实例返回如下内容:

我什至安装了 django-widget-tweaks (这太棒了,顺便说一句!),并尝试这样做:

然而,这令人窒息,因为它无法在另一个模板标签中处理 {{link.id}}。:( 任何建议将不胜感激。在此先感谢!

0 投票
1 回答
115 浏览

django - 用于在主页上发布 cmsplugin-blog 条目列表的模板标签

我将 cmsplugin-blog 与 Django-cms 一起使用。我想在主页中显示最新博客条目的列表(不与插件挂钩)。为此,我创建了一个模板标签。我可以获得一组条目,但不知道如何呈现它们的标题。谁能指出我正确的方向?

这是代码。

cmsplugin_blog_tags.py:

entry_list_1_snippet.html:

0 投票
2 回答
741 浏览

django - 为什么使用 django TemplateTag 标签?

我最近一直在阅读一些 django 代码,并且标签 Templatetag 被大量使用:

与以下较短语法相比有什么优势:

文档说 templatetag 可用于:

对我来说,它只是让代码更长,所以在什么情况下我应该使用它?

0 投票
2 回答
18197 浏览

jquery - html template tag and jquery

I've got this situation where I'm trying to use the <template> tag in my html source:

This ends up placing the template in the document but it is not considered to be in the DOM. This means that $("#content-container") is not found in browsers that support template tags. If I search for:

I get this back:

None of this surprises me. What I need to know how to do is to clone the contents of the document-fragment and have a fresh node I can then stick into the DOM where I want it.

I have found examples of how to do this w/o jQuery but much of the code around this stuff is already using jQuery and I need to know how to use jQuery to do this.

My first thought was to get the html and then use it to create a new node:

This results in the following errors:

I don't know if the error is caused by the mustache syntax or not. I figure there has to be a jQuery solution to this behavior somewhere but when I search with Google I get craploads of hits for jQuery Templates, which are different.

Does anyone have thoughts, answers or pointers to sites/pages that will help me through this? I'm trying to avoid cobbling together something hackish.

EDIT: I ended up finding this solution (I'm still testing it to make sure it IS a solution but it looks promising);

I end up with that containing div that I didn't really need previously but I can live with that. But this kind of feels kludgy. Does anyone have a better approach to this? The upside is that it will still work in browsers that haven't adapted the template tag behavior fully yet.

thanks!

0 投票
1 回答
188 浏览

django - Django,模板

我是 Django 和模板语言的初学者...

这是我的代码:

base.html

news_list.html

0 投票
0 回答
209 浏览

python - 访问 django 模板字典中的列表

嗨,我有一个 Django 模板标签来获取 rss 提要。它非常简单,使用 feedparser 将解码结果放在上下文中。IE

对于特定的 url ' http://feeds.bbci.co.uk/news/england/oxford/rss.xml ' 从 feedparser 打印的第一个条目有一个这样的条目(我删除了其他键以保持这篇文章简短) .

但是,在我的模板标签测试中,元素“media_thumbnail”已更改,因此列表为空。

我的测试用例

返回

(再次删除了其他字段)

我不明白为什么元素已被删除?该词典中还有其他列表。

可以传递给上下文变量的内容是否存在某种键/大小限制?

我知道我可以自定义标签并在 Python 中执行此操作,但我宁愿保持标签通用以与其他提要一起使用。

谢谢

0 投票
1 回答
171 浏览

django - Django 节点属性错误

我正在学习 Django,目前正忙于创建自己的模板标签。我正在尝试创建一些基于带有菜单项的字符串创建菜单的东西。在 Django 模板语言中,以下内容将产生我正在寻找的内容:

{% for item in menu_items %}

{{ item }} {% if not forloop.last %} | {% 万一 %}

{% endfor %}

这是我尝试在指定的模板标签应用程序中输入的内容:

这是我得到的错误:

我基本上是从http://www.djangobook.com/en/2.0/chapter09.html复制整个内容,但这个错误对我来说没有意义。我想我将所有有效属性传递给相关的地方。有什么建议吗?

提前谢谢了!

0 投票
1 回答
870 浏览

python - Django模板标签从错误的日期显示对象

我有一个 Django 模型(db 是 PostgreSQL),其中包含发布日期的 datetime 字段:

还有一个模板标签来获取给定月份和年份的这些对象:

但是当我在模板上使用标签时,日期(以 get_story_month_list 为例)是从发布日期开始的一个月或一年:

任何线索我做错了什么?

0 投票
5 回答
4997 浏览

python - 如何在多列中呈现 form.as_table

我正在使用模型表单,并且我在模板中呈现了该表单,如下所示。

但问题是,它将所有字段呈现为行。由于我的模型包含超过 200 行,当它显示元素为 200 行时,它看起来很糟糕。我想将它们分成 5 列,所以总行数将在 40-43 左右。是否有任何内置模板标签可用于此或我们可以解决问题的任何其他方式。