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?

3
  • 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. Commented 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. Commented 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 file Commented May 9, 2020 at 16:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.