I am writing django templates in Eclipse->prefrences->templates, to autocomplete DJango templates. I wrote this
{% block ${cursor} %}
{% endblock %}
Now, when I request and do autocompletion, after typing {% the autocompletion is
{% {% block %}
{% endblock %}
While I would like
{% block %}
{% endblock %}
With cursor after block. How can I do this?
Instead of typing {% and selecting dj_for_empty, try typing dj_ and then auto-completing. It will behave the way you expect in that case.
BOTTOM-LINE: You auto-complete the templates into the editor based on the template name, not based on the template contents.
It appears that autocompletion has two sources: regular HTML tags (for which I can't find the definitions to change anywhere in Eclipse, sorry) and the templates themselves (which you correctly demonstrated in your comment with the screenshot).
Look at this image:
Instead of typing <t and triggering auto-complete, I typed t. You can see that there are entries with <> - indicating these are autocompletions based on the actual HTML tag - and entries with # - indicating these are autocompletions based on a template.
It appears templates are to be accessed by the name of the template. Notice that the template named table provides a complete <table> and not just the <table></table> that is autocompleted if you just type <tab and autocompletes.
Related
I have a template named 'popup.html' and I have to use this part of code (ie) the html in to my other templates whenever needed. I tried using {% extends 'popup.html' %} whenever needed but it throws me error
PS. I already use {% extends 'layout.html' %} for my navbar inheritance in all pages.
I think the {% include 'template.html' %} tag is what your are looking for.
You can look it up here:
https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#include
I have setup a custom template tag (simple_tag) (using https://stackoverflow.com/a/7716141/1369798) with a definition like this:
templatetags/polls_extras.py
def settings_value(name)
which I am able to use in my template like this:
templates/index.html
{% settings_value "ALLOWED_BOOL" %}
But this just inserts the text into my HTML output.
What is the syntax to use my template tag with parameter in an {% if %}?
I tried this but I get the error: TemplateSyntaxError at / Unused '"ALLOWED_BOOL"' at end of if expression.
templates/index.html
{% if settings_value ALLOWED_BOOL %}
You are allowed.
{% endif %}
You cannot use a templatetag as a parameter to another templatetag. Your options here are either
modifying your settings_value templatetag so it can inject the value in the current context, ie :
{% settings_value ALLOWED_BOOL as allowed_bool %}
{% if allowed_bool %}
You are allowed.
{% endif %}
Note that simple_tag won't work here, you'll either have to switch to assignement_tag (if your Django version support it) - but then you'll loose the ability to directly output a setting in the template the way you actually do - or write a full blown custom templatetag (which is not as difficult as it might seem at first).
Use a custom context_processor instead like danhip suggests - but then only templates rendered using a RequestContext will access these variables.
I'm probably missing something very obvious but can't see anything in the documentation. I have a carousel with a and each will hold an image. However I've added 6 but I want to add an if statement so if an Image has not been added you don't see a blank space, where there is no content inside the .
Here is what i've tried so far:
{% if "Carousel 1" %}
<li>
{% placeholder "Carousel 1" %}
</li>
{% endif %}
Attempt 2:
{% placeholder "Carousel 1" as cara1 %}
{% if cara1 %}
<li>
{{ cara1 }}
</li>
{% endif %}
Not sure if there is something differnt i need to be doing for the django-cms template tags?
Any help would be much appreciated. Docs here - http://docs.django-cms.org/en/latest/advanced/templatetags.html#placeholder
Not to be rude, but your approach is way, way off :)
Placeholders hold Content Plugins. Content Plugins are responsible for how they render their contents.
My advice would be to create or find a carousel content type plugin. This plugin will hold multiple images or "CarouselImage" model instances that you can iterate over, and also specify a template with which to render itself.
In this template resides the conditional statement you're wanting to check for. Placeholders are just that - places held for content plugins.
My friend and I are having a small argument. In my current Django Project, I have created a file called menu.html which will contain a bunch of links configured and formatted into a list. Instead of manually hard-coding the menu into each page, I am currently including the menu using the following Django/Python code:
{% include 'menu.html' %}
However, my friend is suggesting that this is the incorrect way to do it. He said I need to use extends instead of include and then define content, something like this:
{% extend 'menu.html' %}
{% block content %}
The rest of my content here.
{% endblock %}
That's a bit of extra code. Does it really matter which I use? I would prefer to use the former.
Yes, it matters. First of all extends can only occur as the very first line of the file. Secondly, include pushes and pops a context object on the resolve stack, which means that value created in the context while in the include will go out of scope when it returns.
My rule is: create base.html template files that define the overall structure of your site and use liberal amounts of {% block foo %} around critical areas. Then all of your other templates extends the base (or something that itself extends the base) and you replace those blocks as needed.
include, on the other hand, is good for encapsulating things you may need to use in more than one place, maybe even on the same page.
Update:
I have been using my own library of template_tags for so long that I forget that Django's template language still has major gaps in functionality. The tag in question here is from an early django snippet called expr which I have heavily edited and extended. You can say, for example, {% expr 'Fred' as name %} (or any valid Python expression), and it will store the result in the 'name' slot in the current Context. If this occurs in an included template, name's value will be popped on exit from the template file.
You can sort of achieve this with the {% with %} tag, but expr gives me much greater flexibility, including doing arbitrarily complex calls. This originally came up when having to create complex cached objects that required expensive DBMS interactions that couldn't be done up in the view, they had to be invoked in the template itself.
Email me (in my profile) if you need to get deeper into this.
( his friend )
What I actually meant was defining a base.html so you can inherit a base template consistent of several generic sections, this one includes the doctype, html element defines 3 blocks for content and nav and optional area to override/insert script/link elements in the head.
<!doctype>
<html>
<head>
{%block extrahead %} {%endblock %}
</head>
{%block nav %}
<nav>
<ul>
<li>home</li>
</ul>
</nav>
<div id="content">
{% endblock %}
{%block content %}
{% endblock %}
</div>
</html>
Then you can define homepage.html:
{% extends "base.html" %}
{% block content %}
homepage content
{% endblock %}
homepage.html would then have the navigation because it extends base.html.
In this case placing the menu in base.html and extending from this seems to be more senseful.
including is great for splitting complex template and reusing those chunks.
let's say, you use the same list-style in different places of the site, but you send other queryset to it. as long, as you call the querysets the same, you only need to write the template code once.
Here I use differnt templates for normal- and ajax-requests. But using include let me re-use most parts in both templates
I'm working on a site that will have a bunch of pages with an indeterminate amount of "apps" on each page. Something like a calendar app, and a random picture app, or whatever, each in a neat little box. While it's possible to write a template with a bunch of if tags that include other templates, this is a bit of a hassle. I'd like to pass in some variables and have forms on some of these apps, so it would get out of hand quickly. Writing custom inclusion tags will be better than {% include x %}, but it would still be a lot of if statements and writing out every possible app for each page.
Is there any way to loop over something like inclusion tags and include only those that are relevant? Any other completely different solution that I'm missing?
What I'm trying to avoid, whether I use {% include %} or inclusion tags, is this:
{% if apps.calendar %}
{% include "calendar.html" %}
{% endif %}
{% if apps.pictures %}
{% include "pictures.html" %}
{% endif %}
This would mean we'd have to update templates any time a new app was added. What would be nice is something like:
{% for app in apps %}
{% call appropriate include or inclusion tag %}
{% endfor %}
With very few exceptions we use our custom tags all over the place, so we deal with this by simply placing the following in the app/__init__.py file.
from django import template
template.add_to_builtins('content.templatetags.local_tags')
template.add_to_builtins('utils.cachetemplate')
So all of the pages have them available by default. Doesn't seem to impact performance and we use tag names that are unlikely to interfere with other stuff we might include. It's lazy but it works.
Update: OK, I think I better understand what you want. One way of doing this (although I don't really recommend it) is to put the check for the variables inside the included templates. This means you will always have the overhead of including the template, but it will make your other pages marginally less cluttered.
So instead of this in your main file:
{% if apps.calendar %}
{% include "calendar.html" %}
{% endif %}
you simply have:
{% include "calendar.html" %}
and in "calendar.html" you have:
{% if apps.calendar %}
whatever you do for your calendar app
{% endif %}
Update 2:
The last bit of code you show could be done as follows. This takes advantage of the the fact that {% include arg %} "resolves" its argument. This means it can be a variable or a method reference that returns a usable string value that is a template name.
{% for app in apps %}
{% include app %} <!-- or perhaps {% include app.template %} -->
{% endfor %}
Note: Django's template code does not handle top level callables correctly. This means your context cannot pass a reference to a function and expect it to be called and it's output inserted when referenced in a template. Ie. in the example above your list of apps may not be simple functions which you wish to be called. To work as expected a function reference must be either a member of a list or dict, or it must be a method of an object that is passed. We MonkeyPatched this problem long ago because we use curried functions in cached template fragments to defer some heavy DB work. This ticket has been open for over 2 years.