English 中文(简体)
如何在所有配对模板中添加一个方法?
原标题:How to add a method to all django templates?

在我的项目的所有模板中添加类似方法的功能的最佳方式是什么?

我需要的方法如下:

def has_access(user, code):
    return user has access to code

在一个模板中,它像这样使用圆柱:

{% if has_access(request.user,  add-project ) %}
    html code here
{% endif %}

自定义过滤器不会起作用, 因为我没有“过滤”任何东西。 另外, 从我所看到的来看, 自定义上下文处理器不接受额外的参数 。

不知道该怎么在对峙区做这件事

最佳回答

自定义过滤器实际上有效 :

{{ user|has_access:code }}

自定义模板标签将有效 :

{% has_access user code %}

中继器能起作用... 但很难说什么是解决你问题的最佳方法 而没有更多的信息

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签