Django template - set variable in for loop - django

I am using this code in my templatetags:
http://pastie.org/3530409
And I know for context problem and bad design (that this logic should not be in view) but I need in template solution for this:
{% for tag in page.tagname_list %}
{% ifequal tag "wiki" %}
{% set howto = 1 %}
{% endifequal %}
{% endfor %}
So I can use howto variable latter for my view logic.
Is there any way to do this in view templates, without model modification ?
If answer yes, please provide some solution...
Thanks a lot.

Instead of having to set the variable, you could just do:
{% if "wiki" in page.tagname_list %}
# do your wiki stuff below.
{% endif %}

Related

Django dynamic template variable inside if clause

Here's what I'm trying to achieve in "pseudo code":
{% for page in pages %}
{% if 'can_access_page_{{page.name}}' in perms %}
<li>
{{ page.name }}
</li>
{% endif %}
{% endfor %}
How to do this? Permission names I can customize — but still can't figure out this one.
Simplest way is to slightly abuse Django's existing add template filter (intended for numbers but works for strings), as in this answer:
https://stackoverflow.com/a/4524851/202168
You'll need a custom filter. Something like:
#register.filter
def check_page_perms(page, perms):
return 'can_access_page_%s' % page.name in perms
and use it:
{% if page|check_page_perms:perms %}

Read a file in django and find string in file

i am a new to django, and i'm trying to execute a django code in an html template, but it is showing the same error and i dont know how to make it work. I am trying to read some files and find the one that contains the string. This is the code in the template:
{% if documents %}
<ul>
{% for document in documents %}
{% if 'TheString' in open('document.docfile.url').read() %}
<li>{{ document.docfile.name }}</li>
{% else %}
<li></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No documents available.</p>
{% endif %}
The error is the following:
TemplateSyntaxError at /subir/
Could not parse the remainder: '('document.docfile.url').read()' from 'open('document.docfile.url').read()'
What could it be, or am i doing it wrong? Thanks in advanced.
Your problem is you are trying to use Python code in the template, like your open() call.
Django templates don't work like that - you can only use a very limited set of syntax, and the syntax is not the same as Python.
Everything inside {{ }} and {% %} needs to be special Django template syntax, not Python code.
See the Django Template language documentation for the full details.
You can't use code like this in a template directly see the documentation for available template tags and filters. From what I can tell by the code you have posted in your view.py you'll need something like this:
docs=[]
for document in documents:
if 'TheString' in open('/path/to/document').read():
docs.append(document)
after passing docs to the template you could do this in the template:
{% for doc in docs %}
<li>{{ document.docfile.name }}</li>
{% empty %}
<p>No documents available.</p>
{% endfor %}
I'd need to look at what you are passing from the view to be sure of this, but I'd gladly assist in further help with further information if this doesn't help.

django template: how to enclose a variable

I would like to make a sequence of variables within a for loop such as name0, name1, .... How do I do that? Thanks.
{% for i in '1234567890' %}
{% if name{{forloop.counter0}} %}
...
{% endif %}
...
{{name{{forloop.counter0}}}}
...
{% endfor %}
is as simple as
{{ name }}{{ forloop.counter0 }}
for the if, you should use the "with" statement:
{% with name|add:forloop.counter0 as if_test %}
{% if if_test %}
... <!-- do whatever you need to do here -->
all this must be inside your for loop
As you can see, the Django templating language tries hard to keep you from doing what you're trying to do, encouraging you to do your data processing in your view code, instead of your templates. For your example, in your view code, you might try doing:
context['names'] = [name for name in names[:10]]
...instead of creating individual variables for each name.
Then in your template:
{% for name in names %}
{% if name %}
...
{% endif %}
...
{{name}}
...
{% endfor %}
As far as I can tell, that would have the same effect as your code, but you would be doing your aggregation of the names in the view, instead of the template. If I'm reading the intent of your code wrongly, please provide more context, but it doesn't seem like you're doing anything that requires template logic.

Django - Is there a way to create a variable in the template?

I want to do this:
{% for egg in eggs %}
<p>{{ egg.spam }}</p>
{% if egg.is_cool %}
{% myvariable = egg %} // Possible in any way?
{% endif %}
{% endfor %}
Pardon the JavaScript-style comment (it shows up as a comment on SO)
I think the closest you'll get is the with tag: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#with.
If you're say trying to feature an item in a template, I can imagine doing something like:
<div class="special">
{% with some_list.first as special_item %}
{{ specialitem }}
{% endwith %}
</div>
<div class="everything">
{% for item in some_list %}
{{ item }}
{% endfor %}
</div>
If you want some special logic to determine which one is the special item, I'd add a method to the object (so you end up with: {% with some_collection.my_method as special_item %} above) or determine the special item before passing it to the view. Hope that helps.
Welcome to Django Templates.
This problem is easily solved with one of the earliest snippets posted to DjangoSnippets.com: the Expr tag.
People can argue all day about the separation of logic from the templates, but that ignores that there is business logic, which belongs in the models or views, and presentation logic which belongs only in the templates. If you have a lot of presentation logic you may want to consider using Jinja2 for some or all of your templates. WARNING: although Jinja2 looks a lot like Django's template language, there are incompatibilities with things like Custom Template Tags.
Yes, you can use the with construct:
{% with myvariable as egg %}
do stuf
{% endwith %}
I think it's probably best to do this kind of test-and-set behaviour in the view, not the template. If anything, it'll give you better control over cacheing if/when you need it.

what happen with this django-template if tag?

Sorry if I am being a moron, but what happen with this if tag ?
view return:
return render_to_response('productos/por_estado.html', {'productos':productos},
context_instance=RequestContext(request))
#Im not returning 'estado' !
template:
{% if estado %}
{% block activos_active %}class="active"{% endblock %}
{% endif %}
template html result:
class="active"
:S
Block tags cannot be set conditionally, arguably because they are part of template inheritance. I believe this was an explicit design decision by the Django developers. See this Django ticket as well as this other question on Stack Overflow.
One way to set class="active" could be just this
<div{% if estado %} class="active"{% endif %}>....</div>