Can a Django view return a title first?

I have a particularly difficult look, which may need to send a message about the form several times, but I need a header right away.

Is there any way to return part of the template header? for example, my view returns something like:

return HttpResponse(Template('
  {% extends "base.html" %}
  {% block content %} FOO {% endblock %}
'))

Ideally, I want to be able to do something like:

partialResponse = request.renderUntilBlock('content')
# lots of work
return partialResponse.extend(Template('
  {% block content %} FOO {% endblock %}
'))

Update: Obviously, PHP is structured differently, but this is what I hope to emulate:

<?php
echo '<html><head><title>Hi!</title</head><body>';
ob_flush(); flush();
# header has now been output to the client
# do lots of work
echo '<h1>done</h1></body></html>';
?>
+3
source share
3 answers

Do not fully verify this, but this should work as per the docs.

from django.template import Context, Template

def responder():
    yield '' # to make sure the header is sent

    # do all your work

    t = Template('''
        {% extends "base.html" %}
        {% block content %} FOO {% endblock %}
    ''')
    yield t.render(Context({}))

return HttpResponse(responder())
0
source

Yes it is possible. What you need to do is grab each individual render as a string, and then combine the lines to form the full content of the response.

:

from django.template import Context, Template
t1 = Template("My name is {{ my_name }}.")
c1 = Context({"my_name": "Adrian"})
s = t.render(c1)
t2 = Template("My name is {{ my_name }}.")
c2 = Context({"my_name": "Adrian"})  # You could also use the same context with each template if you wanted.
s += t.render(c2)

return HttpResponse(s)

, , , . , - . , .

0

As far as I know, there is no way to do this directly. It’s best to just return the page with only the header and javascript function that retrieves the rest of the page data through AJAX.

0
source

Source: https://habr.com/ru/post/1796199/


All Articles