Call a function in template

Hello everyone,

I’m so new in Django and if I am asking a stupid question, so sorry. But is it possible to reach file from index.html?

I just want to decrypt one field, so that I can show the encrypted data saved in the database to the user as normal format. That’s why I need to call file to use decrypt function in index.html. Thanks in advance.

def decrypt(pas):
      pas = base64.urlsafe_b64decode(pas)
      cipher_pass = Fernet(settings.ENCRYPT_KEY)
      decod_pass = cipher_pass.decrypt(pas).decode("ascii")
      return decod_pass
 except Exception as e:
     return None

What do you mean precisely by “reach it from”?

When in doubt, focus on what’s really happening.

The browser issues a request. The web server directs that request to something that is going to return a response.

Everything that happens, effectively happens in those terms, in that sequence.

Thank you for the answer.

In, I have two functions; encrypt(password) and decrypt(password). I encrypt one field(patientid) value I received via POST and save it to the database in No problem here. And I am sending this value as json format with and display it in datatable(In my case, it is displayed encrypted). I want to decrypt it in server side. After sending encrypted data to server side in json format, I need to decrypt this data. However, I cannot call the decrypt function at this stage. I dont know how to pass decrypt function to my index.html which is in templates.

You don’t call it from the template.

You call the function from the view that is going to render the template - and pass the results to the template in the context.

All your work to prepare a response belongs in the view.

Thank you again for the patience.

I have one more question. Is it safer to decrypt the encrypted data again in and pass it to index.html as you have said, or to decrypt this information in the server side, that is, index.html and display it on the datatable?

Note: I can of course call those two functions in but no idea how to pass that function to template in the context.

I don’t understand your question - decryping the data in the view is decrypting the data in the server.

I mean, I encrypt the patientid data I received via POST in and save it to the database. In order to display that patientid data in datatable, I need to decrypt it.

My question is that it is a good way to decrypt patientid in and pass the result to index.html, or passing encrypted patientid with json format to index.html and do decryption in index.html?

At 2nd case, I cannot pass decrypt function to index.html.

That pretty much answers your question.

Templates are meant to display data - not do any heavy processing on it, that’s why you can’t really call methods inside templates.

1 Like

You don’t decrypt in the template.

You decrypt in the view.

You don’t pass the decrypt function to the template.

You call the decrypt function in the view, and you pass the results to the template, in the same way you supply any data to the template.

1 Like