Question

I have, what I believe, to be some complex processing of my model data that would be easier to take care of inside my view than inside of an html template. Thus, I would be returning the raw html inside of HttpResponse. However, I would still like to get the benefit of template inheritance that render_to_response provides.

Thus, I would like to do something like the following:

{% extends 'base.html' %}
{% block content %}
<!-- The raw HTML I've somehow 'inserted' -->
{% endblock %}

I don't require the use of any other template tags or variable evaluation. Is this possible in Django? How do I best go about doing this?

Était-ce utile?

La solution

The only way to do such thing is to create a 'generic' template, that would contain exactly what you've posted. Just pass the HTML variable to that template (so, yes, render it normally) and mark it with the safe template filter.

EDIT

Well, sorry, it's not the only way to do this. You could also create a decorator that would accept a HttpResponse and wrap its content around, or you could even create a middleware for this, but the above answer seems to be the most simple one.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top