3

I want to do something very simple but I couldn't find any information anywhere: I want to pass some variables to the admin interface of . In simple words, I want to calculate some values:

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

and then use it inside index or base_html.html as {{ sum }}

How can I do something like that?

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Radolino
  • 1,834
  • 4
  • 26
  • 48
  • 1
    in django-speak variables in a template are referred to as *context*. Check out this question: http://stackoverflow.com/questions/9220042/django-how-to-pass-custom-variables-to-context-to-use-in-custom-admin-template – monkut Dec 18 '13 at 06:09
  • indeed, but I do not want to use "context" from inside a class. – Radolino Dec 18 '13 at 06:11

1 Answers1

8

According to my understanding of your question.I hope this will help you. Create this directory structure inside any app. templatetags templatetags/init.py templatetags/sum.py

from django import template

register = template.Library()

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

Now copy a base_site.html inside your template folder from django source code in the structure.

-admin -base_site.html paste this on the top of html

{% load  sum %}

now paste this where you want

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

You can create any function instaed of sum.

Aks
  • 1,152
  • 8
  • 7
  • so django sees them as separate .py programs ? – Radolino Dec 18 '13 at 07:04
  • 1
    You can say.See..so many filters and tags are provided by Django itself.Check it in Django Documentation.It depends what u want to do with the template variables.. if its not provided You can create your own template tag in py file. I just give u an example. Do according to your need dude. – Aks Dec 18 '13 at 07:15