Rendering template object in an if comparison string - django

I've been trying to do this and getting no results
<ul>
{% for topic in topics %}
<li {% if request.get_full_path == "/topic/{{ topic.topic_slug }}/" %}class="is-active"{% endif %}>
{{ topic.topic_name }}
</li>
{% endfor %}
</ul>
The error, I'm guessing is from the {{ topic.topic_slug }} being rendered in the string. I'd like it to be "/topic/tech/" during rendering but that seems not to be working.

I don't believe you can use {{ }} inside a {% %} so you need to do this in a different way. There are many possibilities.
Try using
{% if request.get_full_path == "/topic/"+topic.topic_slug+"/" %}
but the way I would do it is to simply use your class to do the work of returning the path. /topic/tech/ looks like what people usually return from a get_absolute_url class function:
class Topic:
...
def get_absolute_url(self):
return '/topic/{}/'.format(self.topic_slug)
and then in your template, simply use:
{% if request.get_full_path == topic.get_absolute_url %}
(Note that if you already have an absolute_url, simply use another function name. e.g. get_my_topic_slug_url()).

Related

Get a random object of a Wagtail page model but not the current one

So I have this template context processor:
from cases.models import CasePage
def random_case(request):
case = CasePage.objects.live().order_by('?')
return {'random_case': case}
And in the template I do this:
{% for entry in random_case %}
{% if request.get_full_path != entry.get_url %}
{% if forloop.first %}
<a class="ajax-link project-next" href="{{ entry.get_url }}">
<div class="nav-project-title">{{ entry.title }}</div>
<div class="nav-title">next</div>
</a>
{% endif %}
{% endif %}
{% endfor %}
And this works but the problem is sometimes the object is the same as the page so nothing is displayed. It would be great if that one would be skipped in favour of the next entry. And it's also too much logic in the template for me. What would be the best way to move this logic into the context processor and make it work?
Make random_case a method of CasePage, and filter out the case with an ID equal to self.
class CasePage(Page):
# ...
def random_case(self):
return CasePage.objects.live().exclude(id=self.id).order_by('?').first()
You can then refer to this method within your template as page.random_case - bear in mind that a new random choice will be made on each call, so you probably want something like {% with page.random_case as case %}.

Showing random objects in Django template

In my Django template as I am iterating through a list of objects, I'd like to have one list item say:
<li>Blah</li>
and then another do:
<li>Blah</li>
I see value|random as an option but for some reason this doesn't work:
{% ifequal [1, 2]|random 1 %}
{{ post.title }}
{% else %}
{{ post.title }}
{% endifequal %}
Doing this throws this error:
u'ifequal' takes two arguments
Is there any way to accomplish this? I would think it should be simple but I realize the Django templating language doesn't allow for variable assignments.
Thanks!
You can't put a list directly into the template like that, make_list is what you're after.
make_list returns a list of strings, so this would work.
{% if 12|make_list|random == '1' %}
<li>Blah</li>
{% else %}
<li>Blah</li>
{% endif %}

How to only get first object in template

I have the following code, where I get all problem notes.
{% for n in task.task_notes.all %}
{% if n.is_problem %}
<li>{{ n }}</li>
{% endif %}
{% endfor %}
How would I only get the first problem note? Is there a way to do that in the template?
In the view:
context["problem_tasks"] = Task.objects.filter(is_problem=True)
# render template with the context
In the template:
{{ problem_tasks|first }}
first template filter reference.
Would be even better, if you dont need the other problem tasks at all (from 2nd to last):
context["first_problem_task"] = Task.objects.filter(is_problem=True)[0]
# render template with the context
Template:
{{ first_problem_task }}
Assuming you need all of the tasks in the template somewhere else.
You can make a reusable custom filter (take a look at first filter implementation btw):
#register.filter(is_safe=False)
def first_problem(value):
return next(x for x in value if x.is_problem)
Then, use it in the template this way:
{% with task.task_notes.all|first_problem as problem %}
<li>{{ problem }}</li>
{% endwith %}
Hope that helps.
use this code in the loop:
{% if forloop.counter == 1 %}{{ n }}{% 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 %}

Django Many to Many in template

This is my template tag in a forloop
{{ product.feature_set.all.1.value }}
i want to change the number 1 to the forloop.counter. is this posible?
like:
{{
product.feature_set.all.forloop.counter.value
}}
It does not work like that, but is there a way to do this?
This doesn't make sense. You should be looping through the queryset itself.
{% for feature in product.feature_set.all %}
{{ feature }}
{% endfor %}
Since #Daniel's answer doesn't satisfy you, I thought you might want to try writing a custom filter. Here is a rough draft:
#register.filter
def custom_m2m(queryset, forloop_counter):
return queryset[forloop_counter].value
You can use it in your template like this:
{% for ... %}
{{ product.feature_set.all|custom_m2m:forloop.counter }}
{% endfor %}