I have a nested loop to display content and apply terms in my template.
{% for class in class %}
{% if saveclass %}
{% for saveclass in saveclass %}
{% if class.colecl in saveclass.saveclass_tag %}
save class exists!
{{ class.levelclass_name }}<br>
{% else %}
save class not exists!
{{ class.levelclass_name }}<br>
{% endif %}
{% endfor %}
{% else %}
there is nothing save class
{{ class.levelclass_name }}<br>
{% endif %}
{% endfor %}
The first loop gives me the list of classes.
Then it is checked whether the saveclass variable exists or not, if there is, the second loop is executed.
In the second loop it gives me saveclass and I check if the class tag is in the saveclass table and I display the output.
The problem is that the inner circle has to end to get to the next class. And for this reason, both the first if and its else are executed.
Python and other languages use breaks for this, but Django does not have a break template.
What is your solution?
Thank
I researched and according to friends Django template did not allow the break of the ring and should be managed in views.
Related
So I am trying to declare a variable inside of my django templates file
{% with object = "{{object.id
}}" %}
{% for detail in details %}
{% if detail.post.id == {{object}} %}
{{detail.name}}
{% endif %}
{% endfor %}
{% endwith %}
I know that with is used to this job, but when i run this code it shows me this error: 'with' expected at least one variable assignment
Please help me. Thank You
The post_id is likely an int so you should specify object as {% with object=2 %}, you furthermore should not use double curly brackets in a template tag:
{% with object=2 %}
{% for detail in details %}
{% if detail.post_id == object %}
{{ detail.name }}
{% endif %}
{% endfor %}
{% endwith %}
It is however not a good idea to filter in the template, since this is not efficient, and requires more memory, normally you filter in the view.
I tried this if condition to show previous and next posts only if it exists in database.
{% if blog.pk|add:-1 %}
Previous Post
{% endif %}
{% if blog.pk|add:1 %}
Next Post
{% endif %}
but, it kinda ignores the condition and always show next and previous posts buttons owing to the fact that this condition is wrong. How do I fix this?
Please, don't implement this in the template. Templates should be used for rendering logic, not business logic.
You can define a method for example on your Post object to obtain the previous and next item:
class Blog(models.Model):
# …
def get_next(self):
return Blog.objects.filter(pk__gt=self.pk).order_by('pk').first()
def get_previous(self):
return Blog.objects.filter(pk__lt=self.pk).order_by('-pk').first()
Then you thus can use these methods:
{% with prev_blog=blog.get_previous %}
{% if prev_blog %}
Previous Post
{% endif %}
{% endwith %}
{% with next_blog=blog.get_next %}
{% if next_blog %}
Next Post
{% endif %}
{% endwith %}
This will also fetch Blog objects if there is a "gap". So if there is no Blog object with pk=4, then the get_previous of a Blog with pk=5, will be a Blog with pk=3.
I have a django app with a basic model (Job). Now in my template I would like to check if an instance exists of that model or not. I want to project a text if there is nothing yet to show, otherwise I'd like to show the model attributes.
Somehow like so (which obviously doesn't work):
{% if job.title != "" %}
{{ job.title }}
{% else %}
hola
{% endif %}
Also tried:
{% for job in jobs %}
{% if job.title %}
{{ job.title }}
{% else %}
Hola
{% endif %}
{% endfor %}
It makes sense it doesn't work because how can I loop through it or return something if it doesn't exist. Is there a simple way to even do that in a template? Or do I have to write my own function? Or what would be a way to do that?
Help is of course very much appreciated
You can use the {% if %} tag. As Django doc says:
The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output.
So you can do something like this:
{% if job %}
{{ job.title }}
{% else %}
<p>Hi from Uruguay</p>
{% endif %}
If you need this inside a for, as #dirkgroten said, you need to use the {% empty %} tag. There is an example in the Django doc.
I am working on a Django template, and I need to print my statement once during the loop iteration. I've tried {% ifchanged %}, but it's not working inside two loops.
Using {% ifchanged %} works under a single loop, but I am trying this inside two loops.
For example:
{% for i in j %}
{% for k in j %}
{% ifchanged %}
//something here//
{% endifchanged %}
{% endfor %}
{% endfor %}
However, in this case it's not working.
There’s also forloop.first.
See the Django Built-in template tags and filters documentation for the variables associated with forloops.
{% if forloop.first %}
// something here //
{% endif %}
There is also a forloop.last, if it needs to show up at the end, and a forloop.counter.
I'm currently editing the base template that all my Django-CMS templates inherit from. What I'm trying to do is printing the page_title attribute, and if that hasn't been set, print {{ block.super }} instead.
My current code is this:
{% block title %}
{% page_attribute "page_title" as cms_title %}
{% if cms_title and cms_title.strip %}
{{ cms_title }}
{% else %}
{{ block.super }}
{% endif %}
{% endblock %}
Now, when the page title property is set, this works as expected. However, when the page title is left blank, the page's title attribute is used instead which I wouldn't expect.
Is this expected behaviour? If it is, how can I work around this? Or is there another approach to get the result I want?
I'm using Django-CMS 3.0
Edit
I've been told that this is expected behaviour, but no workaround was provided. The solution that was good enough for my situation was to compare the page_title and title attributes. If they're equal, the title is probably being used as a fallback so I could assume the page_title wasn't set.
If anyone has a better suggestion, feel free to answer :)
I've just given this a go & you can conditionally override the block like so;
{% extends 'base.html' %}
{% load i18n cms_tags %}
{% page_attribute "page_title" as cms_title %}
{% if cms_title %}
{% block title %}
{{ cms_title }}
{% endblock %}
{% endif %}