Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

character "@" in the name of a variable in the django template

I have a dictionary in my views.py

mydata = {'@model': 'wolfen', '@genre': 'fantastic', 'price: '350'}

which I pass to the django view in a context like this

context['mydata'] = mydata

and in my view i display like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{{mydata.@model}}

the problem is that the django template uses the "@" character for other tags.
How to replace the "@" character to not display the following error

Could not parse the remainder: '@model' from 'mydata.@model'

thank you

the solution of Willem work fine.

I have another nested dictionary that looks different

mydata_2 ={'1' {'@model': 'wolfen', '@genre': 'fantastic', 'price': '300'} , {'3' {'@model': 'phase4', '@genre': 'fantastic', 'price': '450'} }

the keys of the main dictionaries ("1" , "3") can change dynamically.

otherwise a big thank you to Willem

>Solution :

Rename the data, for example with:

mydata = {'@model': 'wolfen', '@genre': 'fantastic', 'price': '350'}
mydata = {k[1:] if k.startswith('@') else k: v for k, v in mydata.items()}
context['mydata'] = mydata

Then you can use {{ mydata.model }} in the template.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading