0

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?

Amirhosein Shirani
  • 395
  • 1
  • 3
  • 13
  • 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 https://stackoverflow.com/questions/8284765/how-do-i-call-a-javascript-function-from-python is almost the answer you're looking for. – Isolated May 09 '20 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 May 09 '20 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 file – Amirhosein Shirani May 09 '20 at 16:37

0 Answers0