akaariai kirjoitti:
> Hello all,
>
> I have some problems when trying to load only part of the page (<body>
> for example) in AJAX calls without duplicating page structure in
> multiple templates.
>
> I have my base.html (in reality the structure is much more complex,
> but that doesn't matter for this example):
>
> <html>
> <head>...</head>
> <body>
> <div id="content">
> {%block content%}{%endblock%}
> </div>
> <div id="links">
> {%block links%}{%endblock%}
> </div>
> </body>
> </html>
>
> For some AJAX calls, I need to reload the body of the page. That is, I
> need to get the structure of the body to be similar as is in the
> base.html. The problem is I can't find a way to do this without
> duplicating the structure of the page in the AJAX template.
>
> I have tried to put the <body> part inside another template,
> base_body.html, which I could extend in my AJAX calls. I tried to
> include (or ssi) that in base.html. But this way I cannot override the
> content and links blocks when extending directly the base.html. I
> cannot directly use the base.html when doing AJAX call for the body,
> because this way I will get the <html> and <head> elements again
> loaded in my AJAX call.
I did something similiar with two different approaches. Never really
figured out which one was better way to do it:
Define your body like:
{% block body_contents %}
{%block content%}{%endblock%}
</div>
<div id="links">
{%block links%}{%endblock%}
</div>
{% endblock %}
Now there is two ways - add following to your header:
{% extends parent_template %}
And write two templates:
"full.html" which is like:
<html>
<head>
...
</head>
<body>
{% block body_contents %}{%endblock%}
</body>
</html>
And "ajax.html"
{% block body_contents %}{%endblock%}
And in your view you do something like:
if request.is_ajax():
parent_template = 'ajax.html'
else:
parent_template = 'full.html'
return render_to_response('my_template.html', { 'parent_template' :
parent_template, } ... )
Or, create just a full one and
then I did following:
def ajax_render_to_response(request, template_name, dictionary=None,
context_instance=None, **kwargs):
"""
Returns a HttpResponse whose content is filled with the result of
calling
django.template.loader.render_to_string() with the passed arguments.
"""
httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
if request.is_ajax():
return HttpResponse(loader.render_to_string(template_name,
dictionary, context_instance**kwargs), **httpresponse_kwargs)
else:
dojango_template = Template("""
{%% extends "full.html" %%}
{%% block body_contents %%}
{%% include "%s" %%}
{%% endblock body_contents %%}
""" % template_name )
if context_instance:
context_instance.update(dictionary)
else:
context_instance = Context(dictionary)
return HttpResponse(dojango_template.render(context_instance),
**httpresponse_kwargs)
--
Jani Tiainen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---