Custom render function for Django view


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def render(request, template, context=None):
  2. return render_to_response(
  3. template, context or {},context_instance=RequestContext(request)
  4. )
  5.  
  6.  
  7.  
  8. django.views.generic.simple.direct_to_template takes request as the first parameter and does the same thing, in addition to providing URL parameters.
  9.  
  10. Here’s the source, from django/views/generic/simple.py:
  11.  
  12.  
  13. def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
  14. """
  15. Render a given template with any extra URL parameters in the context as
  16. ``{{ params }}``.
  17. """
  18. if extra_context is None: extra_context = {}
  19. dictionary = {'params': kwargs}
  20. for key, value in extra_context.items():
  21. if callable(value):
  22. dictionary[key] = value()
  23. else:
  24. dictionary[key] = value
  25. c = RequestContext(request, dictionary)
  26. t = loader.get_template(template)
  27. return HttpResponse(t.render(c), mimetype=mimetype)

URL: http://www.fishandcross.com/blog/?p=713

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.