Problem while accessing object in array with template language - django

{% for number in appliers|length|times %}
{% if connector|get_by_index:number == commission.id %}
{{appliers|get_by_index:number}}
{% endif %}
{% endfor %}
I have made custom_template filter and it have worked great but the problem is that i can't access appliers[number]'s class variables.
I tried accessing by {{appliers|get_by_index:number.email}} and it wouldn't work... please can any one can tell me how to solve this?

Related

How can I use current object as variable

I'm a complete beginner at django and currently trying to create a library website. Users can issue books, and I want users who have done so, to see 2 extra buttons: unissue and extend. In my mind, this is what I want to be able to write:
{% for b in Borrower %}
{% if b.book == {{book}} %}
{% if b.student == {{user}}%}
#buttons
{% endif %}
{% endif %}
{% endfor %}
Obviously the error happens at {{book}} and {{user}} because it cannot parse the rest. I'm not sure what the correct syntax is to access current object and user being viewed.

Django template nested for loop with if statement not working

I need to have a nested loop in my Django template, where the outer loop goes through a list of objects, and the inner loop goes through a list of those object id's, and I want to only do something for the id's on the inner list, it never executes however. I think it has something to do with the condition for the if statement, because if I replace it with a true statement it works but it doesn't work as it is now
(I have checked to see that the id's overlap)
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
{% if outer.id == inner_id %}
// do something
console.log({{inner_id}});
console.log({{outer.id}});
{% endif %}
{% endfor %}
{% endfor %}
Syntax seems correct. I would just verbosely output everything.
Perhaps it should be something like this:
{% for main_obj in main_obj_list %}
main_obj: {{ main_obj }}
{% for obj_id in obj_id_list %}
obj_id: {{ obj_id}}
main_obj: {{ main_obj.id}}
{% if main_obj.id == obj_id %}
// do something
match: {{main_obj.id}} == {{obj_id}} ;
{% endif %}
{% endfor %}
{% endfor %}
Brother I am also face this problem so my clever mind get some clever solution about this problem we can do that with JavaScript easily so we need to run it in JavaScript and then.
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
if(outer.id == inner_id.id ){
console.log({{inner_id.id}});
console.log({{outer.id}});
//And also if we reserve place in DOM then we can
//change the inner Html of them easily like.
//demo = document.getElementById("demo");
//demo.innerHTML = inner_Id.id or outer.id
}
{% endfor %}
{% endfor %}

In a django template I would like to use the for loop.counter0 to index into a list

I have something like the following:
{% for i in "xxxxxxxxx" %}
...
{% if passedInList.*forloop.counter0|add:"1"* %} Do Something {% endif %}
...
{% endfor %}
Obviously the if statement is incorrect. I have been using this method to create index names in forms for quite some time, but have never had to use the for loop counter index in an if statement.
Is this possible? If so, how would I go about doing so?
Thanks!
Try to use a with statement to save the forloop counter in a context variable, like so:
{% with index=forloop.counter %}
{% if passedInList.index %} do something {% endif %}
{% endwith %}
(Also, seems like you can use forloop.counter instead of forloop.counter0|add:"1")

Django template - set variable in for loop

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 %}

Django template for loop. Member before

I want to create such loop:
{% for object in objects %}
{% if object.before != object %}
{{ object }} this is different
{% else %}
{{ object }} this is the same
{% endfor %}
Based on https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for I can't. Is there really no simple way to do this? Or I just need to use counter and check for objects[counter-1]?
P.S. .before is theoretical and objects is simple query list. I want to take and do something with the loop member that encountered before current loop member.
Check ifchanged template tag
There is a "simple way" to do this: write a custom template tag. They're really not hard. This would probably do the trick (untested):
#register.simple_tag
def compare_objects(object_list):
comparisons = []
for i in range(1, len(object_list)):
if object_list[i] > object_list[i-1]:
comparisons.append('bigger')
else:
comparisons.append('smaller')
return comparisons
The built-in template tags and filters don't make it easy (as of Django 1.4), but it is possible by using the with tag to cache variables and the add, slugify, and slice filters to generate a new list with only one member.
The following example creates a new list whose sole member is the previous member of the forloop:
{% for item in list %}
{% if not forloop.first %}
{% with forloop.counter0|add:"-1" as previous %}
{% with previous|slugify|add:":"|add:previous as subset %}
{% with list|slice:subset as sublist %}
<p>Current item: {{ item }}</p>
<p>Previous item: {{ sublist.0 }}</p>
{% endwith %}
{% endwith %}
{% endwith %}
{% endif %}
{% endfor %}
This isn't an elegant solution, but the django template system has two faults that make this hack unavoidable for those who don't what to write custom tags:
Django template syntax does not allow nested curly parenthesis. Otherwise, we could do this:
{{ list.{{ forloop.counter|add:-1 }} }}
The lookup operator does not accept values stored using with (and perhaps for good reason)
{% with forloop.counter|add:-1 as index %}
{{ list.index }}
{% endwith %}
This code should work just fine as a django template, as long as object has a property or no-argument method called before, and objects is iterable (and '<' is defined).
{% for object in objects %}
{% if object.before < object %}
this is bigger
{% else %}
this is smaller
{% endfor %}