Question

How to avoid producing html files with content like this:

<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

Can I define somehow it in views or forms, so that there is no need for creating this files in templates?

Was it helpful?

Solution

Most class-based views use the TemplateResponseMixin. You can override the render_to_response method to return a HttpResponse object (or any subclass) that doesn't render a template.

class SimpleResponseMixin(object):
    def render_to_response(self, context, **kwargs):
        return HttpResponse(content='<content>', content_type=kwargs.get('content_type', self.content_type))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top