Reputation: 12495
I would like keep reusing some functions I've created on many of my django views so I don't have to write them over and over on every view.
So I created a folder with a __init__.py
in it. In the same folder I also created a myfunctions.py
file where I placed my functions I would like to reuse on my django views.
Here is just a very simple test function I put in myfunctions.py
to see if if I can reuse the function and especially the variable from that function in my views:
def test_function():
test_variable = 1
I would like to just call the test_function()
on a view and to deliver the test_variable value (of 1 in this case) on a template I created. The template for theat view has already a tag {{ test_valiable }}
.
My problem is that when I call the test_function()
on my view I don't see the test_variable
value passed to the {{ test_valiable }}
tag in the template associated with my view.
The way I called the function in my view is:
test_function()
What am I not doing right?
Upvotes: 1
Views: 2431
Reputation: 1791
test_variable is a local variable so you cant see it after function finished. You should rewrite your function to this:
def test_function():
test_variable = 1
return test_variable
And use it in a views.py
def View(request):
result = test_function()
return render_to_response('template', {'test_valiable' : result })
Upvotes: 3
Reputation: 1791
Your question is not very clear.
I think you must use templatetags or contextprocessors as the specific case may be.
Upvotes: 1