The same data in all django templates - django

I have created my text based game and my game pages have structure like this:
{% extends 'base.html' %}
{% block content %}
// content
{% include 'links.html' %}
{% endblock %}
Now I'm thinking about notifying players about a new message at the top of every page, so I have to update only base.html to show the information, however, I have to add data about new messages to every template before rendering. Maybe is there a quicker way to do that?

You probably want to look into Context Processors -> docs. That's how you automatically add stuff to your templates. Here's a detailed example -- it's old, but I think it still works.
Or you can just use the messages framework, which has the appropriate Context Processor implemented and should do what you need here.

Related

Django - the effect on page load speed of using include tag inside for loop

The odds are that this question will be banned, because this forum seems to me a site for "why it doesn't work"-type of questions, rather than "is it a good idea to do what I do" ones. And yet, I am very much concerned about preserving DRY-ness in my code.
I have a Django template which looks like this:
<ol id = 'task_list'>
{% for item in qs %}
{% include 'list_item.html' with item=item %}
{% endfor %}
</ol>
list_item.html:
<li>
{{item}}
</li>
The advantage (at least, for me) of this code: it positively affects DRYness when I have a ajax code which posts a request for creating new items of the list and renders them on the client side subsequently:
JS:
$.post('my_view_url', function(response)
$('#container').append(response);
Django view:
def my_view(response)
#...
return render_to_response('list_item.html',....)
This way, list_item.html helps me use the same HTML code for both initial rendering of existing elements and client-side rendering of newly created items.
The disadvantage is that {% include %} is known to be rather slow.
The question: Is this code pattern not a performance killer in case of paginated rendering of large arrays of data ?
Additional note:
AFAIK, {% block %} is faster than {% include %}. But I've got no idea how to rewrite the code pattern using block.

django context to built-in pages

I have a template that looks something like:
Main Template, home.html:
{% extends "framed.html" %}
<h2> stuff <h2>
framed.html looks something like
{% block header %}
<h1>{{ sitename }}</h1>
{% endblock %}
Normally when I call these views I give it a context with a context containing a key "sitename" assigned get_current_site().name, which works fine.
I also, however, would like to use framed.html at the top of a bunch of templates that are also called by django default views. For example:
return HttpResponseRedirect(reverse('django.contrib.auth.views.login'))
The top of that page never gets the {{sitename}} to show up, so I end up with some blank space at the top of my page. The same goes for flat pages, logouts, etc. Is there a way I can get the relevant context added to all of these "built-in" pages?
You can write your own template context processor that will add required variable in the parameters provided to each template.
More details at Writing your own context processors

Django-CMS - Global placeholder?

Is there any way to make global placeholder in my base template? I need it to be the same on every page (banners list).
How can I do that?
I usually create a page in my CMS that isn't published, but contains placeholders that I would like to use elsewhere (footer/headers) etc.
Make a new template extra_placeholders.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
{% placeholder "Banner-List" %}
{% endblock %}
add it to your settings:
CMS_TEMPLATES = (
('my/path/extra_placeholders.html', 'Extra Placeholder Page'),
...
)
now go to the admin and create the placeholder with whatever plugin you want. Then go to you base template (*base.html probably) from which all your other pages inherit and add this wherever you want the placeholder to appear:
{% load cms_tags %}
...
{% show_placeholder "Banner-List" "extra_placeholders" %}
You can read more about it in the docs
EDIT
As #José L. Patiño has mentioned in the comments, this solution is only necessary for those using django-cms < 3.0. For the newer version you can simply use the static_placeholder template tag
There is the "static_placeholders" now, http://docs.django-cms.org/en/latest/reference/templatetags.html#static-placeholder
Sounds like it's what you needed way back when.
You can use the following ways to create a global palceholder for all pages.
create a place holder on the base page. {% Placeholder "footer"%}
make the contents of the placeholder through django cms as home page
then to display the same for each placeholder page, add {% show_placeholder "footer" "home"%}, this means displaying the newly created footer placeholder earlier from the home page,
This will display the entire contents existing footer placeholders on the home page of all pages that use the template.
but for the home page there will be two footer is displayed, to mengilangkannya, please do modifications to use CSS to hide the master placeholder.

Django conditional template inheritance

I have template that displays object elements with hyperlinks to other parts of my site. I have another function that displays past versions of the same object. In this display, I don't want the hyperlinks.
I'm under the assumption that I can't dynamically switch off the hyperlinks, so I've included both versions in the same template. I use an if statement to either display the hyperlinked version or the plain text version. I prefer to keep them in the same template because if I need to change the format of one, it will be easy to apply it to the other right there.
The template extends framework.html. Framework has a breadcrumb system and it extends base.html. Base has a simple top menu system.
So here's my dilemma. When viewing the standard hyperlink data, I want to see the top menu and the breadcrumbs. But when viewing the past version plain text data, I only want the data, no menu, no breadcrumbs. I'm unsure if this is possible given my current design. I tried having framework inherit the primary template so that I could choose to call either framework (and display the breadcrumbs), or the template itself, thus skipping the breadcrumbs, but I want framework.html available for other templates as well. If framework.html extends a specific template, I lose the ability to display it in other templates.
I tried writing an if statement that would display a the top_menu block and the nav_menu block from base.html and framework.html respectively. This would overwrite their blocks and allow me to turn off those elements conditional on the if. Unfortunately, it doesn't appear to be conditional; if the block elements are in the template at all, surrounded by an if or not, I lose the menus.
I thought about using {% include %} to pick up the breadcrumbs and a split out top menu. In that case though, I'll have to include it all the time. No more inheritance. Is this the best option given my requirement?
You can put your hyperlinks inside a block that is overridden by the loading templates.
Let's say you have your framework.html like this:
{% extends "base.html" %}
<html>...<body>...
{% block hyperlinks %}
your hyperlinks here
{% endblock %}
rest of the code
</body></html>
You can then create something of a nolinks.html template and use it
{% extends "framework.html" %}
{# here you'll have everything from framework
but now we disable the breadcrumbs #}
{% block hyperlinks %}{% endblock %}
If you're getting the past data you can then use nolinks to render instead of framework.
I hope this helps.
From here: Any way to make {% extends '...' %} conditional? - Django
It can be done like this :
{% extends ajax|yesno:"ajax_base.html,main_base.html" %}
Or:
{% extends a_variable_containing_base_template_name %}
Which ever best suited for you.
Regards;

Edit Django User admin template

I need to edit the template shown for editing a specific user. I need to display some additional data that doesn't fit in with an include-style.
I apologise for the short question... but that's pretty much all there is at the moment.
If you can't accomplish what you want with just subclassing admin.ModelAdmin, you can create a directory "admin/auth" in your template directory and put a "change_form.html" in there. In this template you can override blocks that are available e.g. {% block after_related_objects %}.
Have a look at django/contrib/templates/admin/change_form.html to see how they do stuff, e.g.:
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="../../../jsi18n/"></script>
{{ media }}
{% endblock %}
appending stuff to the extrahead block.
Have a look at
django/contrib/admin/templates/admin/auth/user/
This should contain a couple of templates for modifying the users.
You can override these by copying them to TEMPLATE_DIR/admin/auth and then changing them.
Also, have a look # django/contrib/admin/templates/admin/change_form.html
This is the file you'd copy and change (to TEMPLATE_DIR/admin/auth/user/) to override the change form for that model.
I'd override the admin/auth/user/change_form.html template and add a custom template tag to handle whatever queries need to be done to fetch the data you need to display.