Guest User

Untitled

a guest
Jan 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. django template - parse variable inside string variable
  2. vars['current_city'] = "London"
  3. vars['content'] = 'the current city is: {{current_city}}' #this string comes from db
  4. return render_template(request, 'about_me.html',vars)
  5.  
  6. {{content}}
  7.  
  8. from django.template import Template, Context
  9. ...
  10. context = {
  11. 'current_city': 'London'
  12. }
  13. db_template = Template('the current city is: {{current_city}}') # get from db
  14. context['content'] = db_template.render(Context(context))
  15. return render_template(request, 'about_me.html', context)
  16.  
  17. simple_cache = {}
  18.  
  19. def fooview(request):
  20. context = {
  21. 'current_city': 'London'
  22. }
  23. db_template_string = 'the current city is: {{current_city}}'
  24. if simple_cache.has_key(db_template_string):
  25. db_template = simple_cache.get(db_template_string)
  26. else:
  27. simple_cache[db_template_string] = Template(db_template_string)
  28. context['content'] = db_template.render(Context(context))
  29. return render_template(request, 'about_me.html', context)
Add Comment
Please, Sign In to add comment