1
votes

For reducing the number of table I put the values of table as json string and stored it in a text field my problem I override the admin view of the specific model with now I have to render the the text field with json data in well defined tabular format

This is the value of the json string I have to render in the change_form.html that I already overrided [{'name': 'name', 'value': 'dfgdgd', 'label': 'Name used Account', 'type': 'text'}, {'name': 'email', 'value': '[email protected]', 'label': 'Account Email Address', 'type': 'text'}, {'name': 'url', 'value': 'sdfsdfsdf', 'label': 'Web Address (URL)' , 'type': 'text'}]

1
could you post the codes so that people can help you.rumman0786
Use json.loads to convert the json string into dict, then pass it to template.Qiang Jin
What you have posted is not valid json.Burhan Khalid

1 Answers

1
votes

You need to convert the json back into an object, and pass that object to the template:

import json

def my_view(request):
    s = "[{'name': 'name', 'value': 'dfgdgd', 'label': 'Name used Account', 'type': 'text'}, {'name': 'email', 'value': '[email protected]', 'label': 'Account Email Address', 'type': 'text'}, {'name': 'url', 'value': 'sdfsdfsdf', 'label': 'Web Address (URL)' , 'type': 'text'}]"
    s = s.replace("'",'"') # json must be double quoted
    o = json.loads(s)
    return render(request, 'template.html', {'data': o})

Then in your template:

{% for obj in data %}
    {{ obj.name }} - {{ obj.value }} - {{ obj.label }} - {{ obj.type }}
{% endfor %}