I have a simple problem. Below html code return list of all users whose liked blog. How can i exclude request user from list of them ?
<div class="collapse" id="likes">
<ul>
{% for like in instance.likes.all %}
<li>
{{like}}
</li>
{% endfor %}
</ul>
</div>
I tried as : {{like |request.user}} or something like this ,but no result. Thanks in advance
The easiest way to do this is probably using an {% if ... %} to filter out that particular user:
<div class="collapse" id="likes">
<ul>
{% for like in instance.likes.all %}
{% if like != request.user %}
<li>
{{like}}
</li>
{% endif %}
{% endfor %}
</ul>
</div>
Related
I try to create simple TODOList app. Where you can create Project, then create tasks for project, subtasks for tasks and subtasks. I create a template to show task:
<li class='task'>
<div class="collapsible-header" id="task-name"> {{task.title}}</div>
<div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details">
{% include 'ProjectManager/views/control-block.html' %}
<p>{{task.description}}</p>
<ul class="collapsible popout" data-collapsible="expandable" id="subtasks">
{% for sub_task in task.subtasks.all %}
{% include "ProjectManager/views/task_view.html" with task=sub_task %}
{% endfor %}
</ul>
</div>
</li>
You can see i try to create a list of subtask, by using this template recursively, but I got an error:
'RecursionError' object has no attribute 'token'
I found some informations, that i should use variable to store template name, like this:
<li class='task'>
<div class="collapsible-header" id="task-name"> {{task.title}}</div>
<div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details">
{% include 'ProjectManager/views/control-block.html' %}
<p>{{task.description}}</p>
<ul class="collapsible popout" data-collapsible="expandable" id="subtasks">
{% for sub_task in task.subtasks.all %}
{% with node=sub_task template_name="ProjectManager/views/task_view.html" %}
{% include template_name with task=node%}
{% endwith %}
{% endfor %}
</ul>
</div>
</li>
I got an error:
maximum recursion depth exceeded
But at start I wrote wrong:
{% with node=**subtask** template_name="ProjectManager/views/task_view.html" %}
And template display list of subtasks with empty elements (without task.title and description).
Then I tried to put some if condition:
<li class='task'>
<div class="collapsible-header" id="task-name"> {{task.title}}</div>
<div class="collapsible-body" data-task-pk='{{task.pk}}' id="task-details">
{% include 'ProjectManager/views/control-block.html' %}
<p>{{task.description}}</p>
<ul class="collapsible popout" data-collapsible="expandable" id="subtasks">
{% if task.subtasks.all|length %}
{% for sub_task in task.subtasks.all %}
{% with node=sub_task template_name="ProjectManager/views/task_view.html" %}
{% include template_name with task=node%}
{% endwith %}
{% endfor %}
{% endif %}
</ul>
</div>
</li>
But I got new error:
maximum recursion depth exceeded while calling a Python object
How can I do this with Django templates?
Full traceback
In this way I show tasks list:
<div class="card-content">
<ul class="collapsible popout" data-collapsible="expandable" id="main-tasks">
{% for task in project.tasks.all %}
{% include 'ProjectManager/views/task_view.html' with task=task%}
{%endfor%}
</ul>
</div>
The global var's name is task. But your local var is also called task.
{% for task in project.tasks.all %}
{% include 'ProjectManager/views/task_view.html' with task=task%}
{%endfor%}
so i guess what you where trying to do is:
{% for task_local in project.tasks.all %}
{% include 'ProjectManager/views/task_view.html' with task_global_of_next_inheritance=task_local%}
{%endfor%}
but what happened is
{% for task_local in project.tasks.all %}
{% include 'ProjectManager/views/task_view.html' with task_global_of_next_inheritance=task_global%}
{%endfor%}
(using the global instead of the local var)
so you are just making the same call over and over again. if i am right, fix with
{% for task_local in project.tasks.all %}
{% include 'ProjectManager/views/task_view.html' with task=task_local%}
{%endfor%}
What does Django return with using {{ fieldset.fields }}? How can I make it a string?
In my template, I have this:
{% for fieldset in adminform %}
<li> {{ fieldset.fields }} </li>
{% if "nanoadded" in fieldset.fields %}
<li> nanoadded is here </li>
{% else %}
<li> nanoadded is NOT here </li>
{% endif %}
{% endfor %}
Here is what is returned:
[('arri', 'aconcentration', 'acat', 'anotes', 'agtlt', 'id'), ('nanoadded', 'response', 'select_charc')]
nanoadded is NOT here
So I am assuming that the fieldset.fields is not returning a string (even though it looks like a string). How can a make Django see the contents of fieldset.fields as a string? Thanks for you assistance!
It looks like the fields property returns a list that contains two tuples, so you might want to run it through a for loop to check each tuple for the membership of the string 'nanoadded'
Perhaps like this:
{% for fieldset in adminform %}
{% for field in fieldset.fields %}
<li> {{ field }} </li>
{% if "nanoadded" in field %}
<li> nanoadded is here </li>
{% else %}
<li> nanoadded is NOT here </li>
{% endif %}
{% endfor %}
{% endfor %}
I would like to make a pagination with Bootstrap : a new page every 10 new field in data.
file.html
{% for d in data %}
{% if forloop.first %}
<ul class="pagination">
{% endif %}
{% if (forloop.counter % 10) == 0 %}
<li>{{ forloop.counter % 10 }}</li>
{% endif %}
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
output I would like that => Bootstrap pagination
But Django give me an error for this :
{% if (forloop.counter % 10) == 0 %}
TemplateSyntaxError :/
I don't know how to do except create my own filter or add a filter, but i would like to know first if i can do in the template first.
PS: I use Django 1.5 and I can't upgrade it.
Edit:
Finally I use this condition:
{% if forloop.counter|divisibleby:'10' and forloop.counter|divisibleby:'5' and forloop.counter|divisibleby:'2' %}
Like that I know when I have a 10 multiple.
The modulus (%) operator is not available in django templates. However, you can use the divisibleby (https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#divisibleby) template filter, something like
{% if forloop.counter|divisibleby:"2" %}
Use the paginator, your QuerySet are not evaluated for the hole table, just the number you need to build the page, and it offers properties that you can use in the template like (page_range, next_page_number, has_next, etc.)
here is the code withe BootStrap 2 and django.core.paginator:
<div class="pagination pagination-centered">
<ul>
{% if MYDATAENTIRES.has_previous %}
<li>
{% trans "Précédent" %}
</li>
{% endif %}
{% for i in MYDATAENTIRES.paginator.page_range %}
<li {% ifequal MYDATAENTIRES.number i %} {{ 'class="disabled"' }} {% endifequal %}>
<a href="?page={{ i }}">
{{ i }}
</a>
</li>
{% endfor %}
{% if MYDATAENTIRES.has_next %}
<li>
{% trans "Suivant" %}
</li>
{% endif %}
</ul>
</div>
I have made it possible for users on my website to upload a post, and to see all the other posts from other users as well. The below code attaches the user, who wrote the post, userpicture.
I wanted it to be a link to that user. My problem is that the below code links to the current user, and not the user which created the post.
Anyone who has some ideas of how to fix this? Thank you!
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ user.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ user.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
In your item model, add a field username, which stores the username of the user who made this post.
Here, it creates a link to the current user, because "user" doesn't have any information about the post. It has information about the current user.
After adding the 'username' field,
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ item.sender.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ item.sender.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
UPDATE:
I have change to item.sender.username. This should work.
The problem is that you are referencing the user object in your link as <a href='/members/{{ user.username }}'>, the correct url should be something like <a href='/members/{{ item.sender.username }}'> but it requires you to have a ForeignKey reference to the user in the item model.
I have the following code in my template:
{% for object in object_list %}
{% with game=object.game %}
{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
<div class='game_link' id='dark_gamelink'>
<a class='img_link' href="{% url game_view game.id game.title|slugify %}">
<img class='game_img' src='{{game|thumb:"78x65"}}' alt='{{game.title}}' />
</a>
<div class='top_game_title' style='padding:0'>
<a style='position:relative; top:-3px' id='yellowlink' href="{% url game_view game.id game.title|slugify %}">{{game.title}} -- {{category.name|title}}</a>
<img style='position:relative; top:1px; margin-left:12px' src='thumbsup.gif' width='17' height='18'/>
<span style='position:relative; top:-3px; font-size:10px; color:white'>99%</span>
</div>
{% if game.description|length > 65 %}
{{ game.description|slice:"65" }}...
{% else %}
{{ game.description }}
{% endif %}
</div>
{% if forloop.counter0 == 3 %}
<div class='more_games'><br/></div><div class='side_header' id='dark_gamelink_side'><a class='adventureheader' href=adventure.htm></a></div>
{% endif %}
{% endif %}
{%endfor%}
{% endwith %}
{% endfor %}
Now I'm using this:
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
to try to detect if this is the first iteration of the for loop immediately preceding it not the parent forloop. In other words I'm trying to detect if it's the 1st iteration of this for loop:
{% for category in object.game.objectmeta.categories.all %}
not this one:
{% for object in object_list %}
The way it is now isn't working because it's displaying this:
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
Twice. How to detect the first iteration of the nested forloop?
Edited:
I have never used these variables but I think forloop.parentloop.first should do it. If not blame me to have misunderstand the Django docs. ;-)
You should check if you are within the parentloop and and then within the first nested node. Please try this modified template. It should you give the right direction.
{% if forloop.parentloop.first %}
I am in the first loop of the parent
{% else %}
{% if forloop.first %}
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% endif %}
{% endif %}
I think the best way to solve this isn't to detect if this is the first iteration in the loop, but rather to write your HTML so that is outside the loop entirely.
You should only be writing HTML elements in the for loop that you actually want repeated for each iteration. If that doesn't work, rethink how you're providing the data to your view (object_list, game, category, etc) so that you can write your markup more easily.
The beginning of your view will probably look something like this:
<div class='side_header' id='dark_gamelink_side'>
<a class='actionheader' href=""></a>
</div>
{% for object in object_list %}
{% with game=object.game %}
{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}