在 Django 模板中调用 Python 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:50:36  来源:igfitidea点击:

Calling Python function in Django template

pythondjangodjango-templates

提问by The.Anti.9

Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this:

在 django 模板中,我试图在模板变量之一上调用 split 函数,然后获取最后一个元素,所以我做了这样的事情:

{{ newsletter.NewsletterPath.split('/').-1 }}

Unfortunately, it doesn't like the split. Some might suggest that I do the split in the view, but I'm not sure how to do that because I need to do it for all of the records. It would be much easier if I could do it in the template. Is there a way to do this?

不幸的是,它不喜欢分裂。有些人可能会建议我在视图中进行拆分,但我不确定该怎么做,因为我需要对所有记录进行拆分。如果我可以在模板中完成它会容易得多。有没有办法做到这一点?

采纳答案by shylent

What do you mean by "it doesn't like the split"? How does it manifest its dislike?

“它不喜欢分裂”是什么意思?它如何表现它的厌恶?

If I remember correctly, you can not pass any arbitrary arguments to methods, that are called from the django template and the identifiers, that can be used in the templates can only consist of a-z, A-Z, 0-9, underscores and dots (where dots signify lookup: dictionary->attribute->method->list-index).

如果我没记错的话,您不能将任何任意参数传递给从 django 模板调用的方法,并且可以在模板中使用的标识符只能由 az、AZ、0-9、下划线和点组成(其中点表示查找:字典->属性->方法->列表-索引)。

There are at least four ways to achieve what you want:

至少有四种方法可以实现你想要的:

  • make the appropriately prepared data available as an attribute of your model (or whatever that is), by pre-processing it
  • make the data available as a method of your model and make sure, that the method takes no required arguments, besides self
  • populate the model instances in the view

     for newsletter in newsletters:
          setattr(newsletter, 'basepath',
                  newsletter.NewsletterPath.split('/')[-1])
    

    (or something along these lines)

  • implement a custom filter tag, that will handle the split (easier, than you might think)
  • 通过对数据进行预处理,使适当准备的数据可用作模型(或其他任何东西)的属性
  • 使数据可用作模型的方法,并确保该方法不带任何必需的参数,此外 self
  • 在视图中填充模型实例

     for newsletter in newsletters:
          setattr(newsletter, 'basepath',
                  newsletter.NewsletterPath.split('/')[-1])
    

    (或类似的东西)

  • 实现自定义过滤器标签,它将处理拆分(比您想象的更容易)

回答by 3lectrologos

From the django book:

Django 书

Note that you do not include parentheses in the method calls. Also, it's not possible to pass arguments to the methods; you can only call methods that have no required arguments.

请注意,方法调用中不包括括号。此外,无法将参数传递给方法;您只能调用没有必需参数的方法。

So, if you want to call a method without arguments from a template, it's fine. Otherwise, you have to do it in the view.

所以,如果你想从模板调用一个没有参数的方法,那很好。否则,您必须在视图中执行此操作。

回答by Ofri Raviv

Templates are deliberately not able to do such stuff. The purpose is to prevent you from putting your business logic in templates, which are meant to deal only with the layout.

模板故意不能做这样的事情。目的是防止您将业务逻辑放在仅用于处理布局的模板中。

So a possible way to do this is to define a NewsletterPathLastElement(self) function in your newsletter Model, and call that from the template.

因此,一种可能的方法是在您的时事通讯模型中定义一个 NewsletterPathLastElement(self) 函数,并从模板中调用它。

回答by Lakshman Prasad

Yes, as others have said, you shouldn't really be doing it in the templates.

是的,正如其他人所说,您不应该真正在模板中执行此操作。

But if you want to, then you need to define a filter and load it in the template and use it.

但是如果你愿意,那么你需要定义一个过滤器并将它加载到模板中并使用它。