1

I have a json string that contains a variable.

"country": {
    "name":"England",
    "city":"London",
    "description":"This country has a land mass of {{ info.1 }}km² and population is big as <span class=\"colorF88017\">{{ info.2 }} millions</span>.",
    "info":[
        null,
        [130395],
        [5479]
    ]
}

As you see those variables are linked to a list in the json file. But when i do in the template html: {{ country.description }} It does not show what info.1 or info.2 contains. It just display everything as a text.

How can i display the value from a variable inside a string?

from django.template.loader import render_to_string
def country_info(request):
    context = {}

    show = request.GET.get("show")
    if show:
        context["country"] = get_country_json()

    return render(request, 'country_info_index.html', context)

Thanks

4
  • Where this json is created? is it in views file? Commented Feb 23, 2019 at 7:54
  • No json is fetched from a website api Commented Feb 23, 2019 at 7:54
  • How are populating this in api? in api you need string contactination, but you are not doing that. And value in {{ country.description }} will always be pure text for django template Commented Feb 23, 2019 at 7:59
  • Sorry my fault, i mean from a json file in a cdn online. I read the json from the backend and render it in the frontend. Ofcourse it shows the description but does not populate the info.1 or info.2 Commented Feb 23, 2019 at 8:01

1 Answer 1

2

Use render_to_string pass the arguments like follow

description_template.html:
This country has a land mass of { info.1 }km² and population is big as <span class=\"colorF88017\">{ info.2 } millions</span>.

use this template in render_to_string

from django.template.loader import render_to_string
template_name = "description_template.html"
description = render_to_string(template_name, 
  context={
     "info": description
  }
)

or using f string

description =  f"This country has a land mass of { info.1 }km² and population is big as <span class=\"colorF88017\">{ info.2 } millions</span>."
Sign up to request clarification or add additional context in comments.

9 Comments

I dont really understand, can you explain bit more. Also description seems not being used. Also i use the shortcut render which seems to use render_to_string
@Printer It parses the string before passing it to the template.
@dan-klasson My computer internet is gone, so i can't test it right away. But should i make a templet filter and Passing the description as value and return it as render_to_string
@Printer Yes, that would be the better option in terms of separation of concerns. But either way works.
Cheers man, will give it a try and Hopefully it will work. Just need wait for my internet to come back.
|

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.