I'm working on a project. I have a strings.py which has dicts like below:
def bar(self):
#do something
STRINGS_ENGLISH = {
'title': 'foo'
'function': bar
}
then I wanna use this string in my Django template, I'll pass it through my context in the template named string.
I know that If I wanna print the title in the HTML it's enough to write {{strings.title}},
but about the function I want to pass it to a button like that:
<button onclick="{function()}">Do Something</button>
but it's a python function! not a js function. so what should I do?
asked May 9, 2020 at 16:30
Amirhosein Shirani
3931 gold badge3 silver badges14 bronze badges
-
Is Python powering your back-end, JavaScript front? You wouldn't typically need to mix python with JavaScript, what would be the benefit? If it's a back-end application, then your logic should be separated. It is possible however, this stackoverflow.com/questions/8284765/… is almost the answer you're looking for.Isolated– Isolated2020年05月09日 16:33:55 +00:00Commented May 9, 2020 at 16:33
-
you cannot call a python function because django is backend and javascript is frontend. You need to look into XMLHttpRequests to do what you want to do.ezra– ezra2020年05月09日 16:34:37 +00:00Commented May 9, 2020 at 16:34
-
yes I know, I try to use javascript just for my Django-template. the benefit of this string collection is the harmony that the strings of a template file give me. I load all of my data from a string file and now I try to load also functions from my strings fileAmirhosein Shirani– Amirhosein Shirani2020年05月09日 16:37:14 +00:00Commented May 9, 2020 at 16:37
default