如何在django模板的“for”函数中的对象上使用python ?

时间:2021-11-27 20:21:15

I want to be able to extract some data in python based on an object in a "for" function in a template.

我希望能够基于模板中的“for”函数中的一个对象,在python中提取一些数据。

In the example below I would like to do something with example outside the template system, so I wont be limited by the template language, but can instead use python:

在下面的例子中,我想用模板系统之外的例子做一些事情,所以我不会受到模板语言的限制,而是可以使用python:

#django_template.html

{% for example in queryset %}
    #do something to example with python
{% endfor %}

I know it might be possible with a template tag, but was wondering if there wasn't a easier way. Is this possible?

我知道使用模板标签是可能的,但我想知道是否有更简单的方法。这是可能的吗?

EDIT: I am able to do this in the view:

编辑:我可以在视图中这样做:

for example in queryset:
    #Extract data based on example

But the problem is i can't use "{% for example in queryset %}" in the template and then get the data I made for each specific example in the view.

但问题是,我不能在模板中使用“{%,例如在queryset %}中”,然后获取视图中每个特定示例的数据。

2 个解决方案

#1


2  

That's not the way Django works. If you want to do something "outside" the template language, you do it in your view function.

Django不是这样工作的。如果你想在模板语言之外做一些事情,你可以在你的视图函数中做。

The template language is intentionally limited to presentation only.

模板语言故意只局限于表示。

No processing.

没有处理。

(You can fudge the rules a bit with properties and descriptors, but don't. It ruins performance and confuses the rest of us. Do you processing in your view functions only, please.)

(您可以用属性和描述符对规则进行一些模糊处理,但不要这样做。它会破坏性能,让我们其他人感到困惑。你只处理你的视图功能吗?

#2


0  

If 'example' is an instance of a class that you control, then you can invoke arbitrary class methods within the template, so that if there's something you want to do like using a special pretty printing method, you can call the method by simply inserting {{example.myfunction}}.

如果'example'是一个您控制的类的实例,那么您可以在模板中调用任意的类方法,这样,如果您想做一些事情,比如使用一个特殊的漂亮的打印方法,您可以通过插入{example.myfunction}来调用这个方法。

Beyond that, you cannot write any python code inside the template itself, so the answer to your question as written is no. You correctly state that you can implement python extensions to the template language using custom tags, however.

除此之外,您不能在模板内部编写任何python代码,因此您的问题的答案是no。但是,您正确地声明可以使用自定义标记实现模板语言的python扩展。

Your use of 'calculate data' is a bit scary -- you definitely shouldn't be doing anything remotely heavyweight like math or I/O from a template. Keep those calculations in your views: this separation is by design.

你对“计算数据”的使用有点吓人——你绝对不应该从模板中做任何重量级的事情,比如math或I/O。在你的观点中保留这些计算:这种分离是设计出来的。

#1


2  

That's not the way Django works. If you want to do something "outside" the template language, you do it in your view function.

Django不是这样工作的。如果你想在模板语言之外做一些事情,你可以在你的视图函数中做。

The template language is intentionally limited to presentation only.

模板语言故意只局限于表示。

No processing.

没有处理。

(You can fudge the rules a bit with properties and descriptors, but don't. It ruins performance and confuses the rest of us. Do you processing in your view functions only, please.)

(您可以用属性和描述符对规则进行一些模糊处理,但不要这样做。它会破坏性能,让我们其他人感到困惑。你只处理你的视图功能吗?

#2


0  

If 'example' is an instance of a class that you control, then you can invoke arbitrary class methods within the template, so that if there's something you want to do like using a special pretty printing method, you can call the method by simply inserting {{example.myfunction}}.

如果'example'是一个您控制的类的实例,那么您可以在模板中调用任意的类方法,这样,如果您想做一些事情,比如使用一个特殊的漂亮的打印方法,您可以通过插入{example.myfunction}来调用这个方法。

Beyond that, you cannot write any python code inside the template itself, so the answer to your question as written is no. You correctly state that you can implement python extensions to the template language using custom tags, however.

除此之外,您不能在模板内部编写任何python代码,因此您的问题的答案是no。但是,您正确地声明可以使用自定义标记实现模板语言的python扩展。

Your use of 'calculate data' is a bit scary -- you definitely shouldn't be doing anything remotely heavyweight like math or I/O from a template. Keep those calculations in your views: this separation is by design.

你对“计算数据”的使用有点吓人——你绝对不应该从模板中做任何重量级的事情,比如math或I/O。在你的观点中保留这些计算:这种分离是设计出来的。