From frappe.call to jinja

From a custom website webpage.html I have triggered an onclick which run a frappe.call script to a custom method in the webpage.py file.
The frappe.call successfully return a callback.

How do I use the callback with jinja (as jinja variable)?

PS: I also has get_context(context) method in the webpage.py file.

Thank you

Hi @rahy,

I’ll try to give a high level overview of what happens. Maybe you can figure out the implementation details yourself.

When you open your page, the browser sends a request to the backend, there the get_context method is called, the Jinja template is rendered, and the HTML sent back to the browser.

Now in the browser, you click on something, which calls a different backend method and sends you the result. But the Jinja template is already rendered in the browser. You cannot render it in the browser again, with different variables.

You have two choices:

  1. When you get back your result from frappe.call, modify your rendered page directly with JavaScript. You can change everything, but the code doesn’t look very nice.
  2. Change your logic, so that the button click reloads the page with different parameters. Move the logic from your frappe.call into your get_context method.

You can also combine the two approaches for different scenarios.

1 Like

If you decide to approach this on the front end using JavaScript, you might also consider using Vue, which Frappe is increasingly supporting.

#1. I have succeeded in using JS to change the rendered page, but I prefer server side code :smiley: when possible.

#2. I tried to frappe.call to the get_context() method but what is the args to be used?

Can I return another context from other method if there is already get_context() method?

Actually, I try to frappe.get_doc('Doctype', docname) and I need to pass the docname that I get using onclick=function(this).
So I don’t think this is possible to use get_context because get_context is before the page is rendered fully.

Well, I’m not a programmer so the most I can use are html, jinja, py, js all from the documentation :smiley:
But I have gone quite far in customising frappe/erpnext :+1:

Correct.

If you want to do this server side, you need to reload the page, passing a new parameter with whatever data you need the page renderer to have.

One way would be to use a url query string. For that, you would reload the page as:
https://erp.example.com/webpage.html?parameter=value

I’m not able to check this right now to identify exactly where, but I believe that your should be able to find parameter: value in the frappe.request object.