1

I am trying to use json.loads in my template in order to get a python's dictionnary. But unfortunately, i encounter the same error :

Could not parse the remainder: ' = json.loads(entry.saved_data)' from 'everyEntry = json.loads(entry.saved_data)'

Here is my code from the template :

 <tbody>
                <tr> 
                  {% for entry in entries %}

                {{everyEntry = json.loads(entry.saved_data)}}
                {{ everyEntry.items}}

                {% for clef, valeura in headersLoop %}
                {% for chiave, valore in everyEntry %}
                {% if clef = chiave %}
                <td>
                  <p>{{clef}} {{valore}}</p>
                </td>
                {% endif %}
                {% endfor %}
                {% endfor %}
                {% endfor %}    
                </tbody>  

An at this line :

 {{everyEntry = json.loads(entry.saved_data)}}

the problem occurs.

How may i fix this ?

Thank you guys.

4 Answers 4

5

Method 1 - Define custom model method

Create custom method in models.py,

class YourModel(models.Model):
   # your stuff

   def entry_saved_data(self):
      return json.loads(self.saved_data)

Method 2 - Using template tags

You can use template tags for doing so.If you are now aware of template tags you can find it in docs

@register.simple_tag
def entry_saved_data(value):
   return json.loads(value)

Use it in template like,

{% load tags %}
{% entry_saved_data entry.saved_data as everyEntry %} 

you now have json deserialized data in everyEntry variable.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay i implemented @register... in my views.py Then i called it with {% load tags %} in the template.html but it shows me an error : "'tags' is not a valid tag library: Template library tags not found," I used the same syntax that you did. Maybe i put the first code (@register...) in the wrong folder ?
how you created the templatetags folder and you created 2 files named init.py and tags.py ? Have you looked the above documentation link ? Anyways @bruno said defining method in models.py is also nice way.
I just read the line that talk about it. Thank you for your help and your patience. :) I check also bruno's method.
Yeah bruno desthuilliers's suggestion is also nice and easy way to do it.
4

Do not write logic in templates.

Do json.load in view and pass it to template via context.

5 Comments

Yes, i know this fact. But, that's not what i expect. :) I'm trying to print all the entry transformed into python dictionnary first. Then changed into items in order to sort them with my headers' value. So if i use json.loads in view, i will only get one entry and not everyone.
Why not prepare the data in the view and only render data in the template?
Or add a method to your entry class that returns the parsed json data ?
That an idea yes ! I will try everything you said and i'll let u know.
I am trying to add a method in the models.py but i can't get all the entries transform into dict items. I can only get one entry which is in dict items. But not all of them. I stuck to this.
1

Thanks to bruno and Aniket, i found the solution.

First i created a method that returns a dict items such as bruno example :

class YourModel(models.Model):   

`def entry_saved_data(self):

return json.loads(self.saved_data)

Then i made this loop into my template.html :

  <tr>
                {%for entry in entries%}
                {%for cle, valeur in entry.all_form_entry_json%}
                {%for key, valor in headersLoop%}
                {%if key == cle%}


                 <td> {{valeur}}</td>

                {% endif %}
                {% endfor %}
                {% endfor %}                   
                {% endfor %}                    
                </tr>

It prints all the datas. But one small problem appears. It doesn't fill the array in a proper way. Nevertheless all the datas from all the forms are printed.

:)

Comments

0

see: (functions in django templates)

Templates in django do not permit calls to arbitrary functions, that that is by design to the best of my knowledge.

That being said, you may be able to create a custom template tag that does what you want.

https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/

2 Comments

Okay I check everything then i'll let you know.
@aniket's code is probably the simplest solution. Make sure to look at "Auto Escaping Considerations" if you have HTML embedded in your JSON.

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.