2013-12-18 63 views
1

我想做一些非常簡單的事情,但我無法在任何地方找到任何信息:我想將一些變量傳遞給的管理界面。在簡單的話,我想計算一些數值:如何將自定義變量值傳遞給django管理界面?

def sum(a,b): 
    sum = a + b 
    return sum 

,然後用它的內部指標或base_html.html爲{{ sum }}

我怎麼能這樣做呢?

+1

django-speak變量在模板中被稱爲* context *。看看這個問題:http://stackoverflow.com/questions/9220042/django-how-to-pass-custom-variables-to-context-to-use-in-custom-admin-template – monkut

+0

確實,但我做不想在類中使用「上下文」。 – Radolino

回答

7

根據我對你問題的理解,我希望這會對你有所幫助。 在任何應用程序內創建此目錄結構。 templatetags templatetags/初始化的.py templatetags/sum.py

from django import template 

register = template.Library() 

@register.simple_tag 
def get_sum(a, b): 
    return a+b 

現在您的模板文件夾內從Django的源代碼結構複製base_site.html。

-admin -base_site.html 此粘貼HTML

{% load sum %} 

的頂部,現在這個貼在要

{% with a=10 b=90 %} 
    Sum is here: {% get_sum a b %} 
{% endwith %} 

您可以創建任意功能instaed總和。

+0

因此django將它們視爲單獨的.py程序? – Radolino

+1

你可以這麼說。參見。因此,許多過濾器和標籤都是由Django自己提供的。在Django文檔中檢查它。它取決於你想用模板變量做什麼..如果它沒有提供你可以創建自己的模板標籤py文件。我只是舉個例子。根據你的需要做老兄。 – Aks

相關問題