Django get_rss template tag - django

I'm trying to use get_rss template tag in Django.
The following code works for me:
{% for feed in feeds %}
{% get_rss "http://rss-url" as rss %}
{% for entry in rss %}
{% include "rss.html" %}
{% endfor %}
{% endfor %}
but when i try to substitute with variable it doesn't output anything:
{% for feed in feeds %}
{% get_rss "{{ feed }}" as rss %}
{% for entry in rss %}
{% include "rss.html" %}
{% endfor %}
{% endfor %}
Is there workaround for it?

you are giving in string inside string. try this
{% for feed in feeds %}
{% if feed %}
{% get_rss feed as rss %}
{% for entry in rss %}
{% include "rss.html" %}
{% endfor %}
{% endif %}
{% endfor %}

Related

Jinja html code gets incorrectly formatted when saved in VS code

I am writting a Django project using VS Code. I find a strange Jinja formatting issue.
What I want is
{% extends "../base.html" %}
{% load static %}
{% block title %}
{% if category %} {{ category.name }} {% else %} Products {% endif %}
{% endblock %}
{% block content %} {% endblock content %}
But when I save the file or ctrl+s, it gets formatted like
{% extends "../base.html" %} {% load static %} {% block title %} {% if category
%} {{ category.name }} {% else %} Products {% endif %} {% endblock %} {% block
content %} {% endblock content %}
and the render gets error due to broken brackets.
I am using Jinja2 Snippet Kit and Prettier plugins. Do I need to make some changes in setting?
Could someone give me a help? Thank you.

how to do assignments for variable in django template tag

I want to do assignment in Django template tag for this code:
{% for ins in ob %}
{% if ins.heur = 'here' %}
a==1
some stuff ..
{% endif %}
{% endfor %}
{% if a!=1 %}
stuff
{% endif %}
You can use the {% with %} template tag.
Example:
{% with a=1 %}
{{ a }}
{% endwith %}

Django template: check for empty query set

Is there a way to check for an empty query set in the Django template? In the example below, I only want the NOTES header to be displayed if there are notes.
If I put an {% empty %} inside the "for" then it does display whatever is inside the empty tag, so it knows it's empty.
I'm hoping for something that does not involve running the query twice.
{% if notes - want something here that works %}
NOTES:
{% for note in notes %}
{{note.text}}
{% endfor %}
{% endif %}
Clarification: the above example "if notes" does not work - it still displays the header even with an empty query set.
Here's a simplified version of the view
sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))
return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))
Edit: the view select selects from multiple tables.
Have a look at the {% empty %} tag.
Example from the documentation
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Link: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty
If you are interested in a table, or some kind of heading if there are results, add the forloop.first:
{% for athlete in athlete_list %}
{% if forloop.first %}
Athlete Name:
{% endif %}
{{ athlete.name }}
{% empty %}
Sorry, no athletes in this list.
{% endfor %}
Try {% if notes.all %}. It works for me.
In your view check whether notes is empty or not. If it is then you pass None instead:
{"notes": None}
In your template you use {% if notes %} as normal.
It's unfortunate that you're stuck using a raw query set - they're missing a lot of useful behavior.
You could convert the raw query set into a list in the view:
notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))
Then check it as a boolean in the template:
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
You could also make it happen without conversions using forloop.first:
{% for note in notes %}
{% if forloop.first %}
Header
{% endif %}
{{ note.text }}
{% endfor %}
What about:
{% if notes != None %}
{% if notes %}
NOTES:
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
{% else %}
NO NOTES AT ALL
{% endif %}
Your original solution
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.
Often the right way to do this is to use the {% with ... %} tag. This caches the query so it runs only once and also gives you more flexibility with your markup than using {% empty %}.
{% with notes as my_notes %}
{% if my_notes %}
<ul>
{% for note in my_notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}
With this particular example I'm not sure how useful it is but if you're querying Many-to-Many field, for instance, it's likely what you want to do.
Use {% empty %} in django templates
{% if list_data %}
{% for data in list_data %}
{{ data.field_1 }}
{% endfor %}
{% else %}
<p>No data found!</p>
{% endif %}
We can write above code with {% empty %}.
{% for data in list_data %}
{{ data.field_1 }}
{% empty %}
<p>No data found!</p>
{% endfor %}

Optimizing a django template full of {% regroup %} tags

I have a Django template full of nested {% regroup %} tags, and I have the feeling that it is one of the reason why it is so slow. Moreover, it is very complex to maintain like this. Do you have any advise and design patterns to make it faster and more sustainable? I'm open to any suggestion, including moving the logic to models or creating template tags...
Here is a snippet of code, with only the structure, to give you a taste of what it looks like...
{% extends "base.tex" %}
{% block content %}
{% for courseoutline in courseoutline_list %}
...
{% regroup courseoutline.coursemembership_set_by_teaching_unit by teaching_unit as course_list %}
{% for course in course_list %}
{% regroup course.list|dictsort:"course.title" by course.title as course_sublist %}
{% for course in course_sublist %}
{% with course.list|semester:1 as cc %}
...
{% for c in cc %}
...
{% endfor %}
{% endwith %}
{% with course.list|semester:2 as cc %}
{% for c in cc %}
{% endfor %}
{% endwith %}
{% endfor %}
{% endfor %}
{% regroup courseoutline.coursemembership_set_by_semester by semester as semester_list %}
{% for semester in semester_list %}
{% regroup semester.list by teaching_unit as ue_list %}
{% for ue in ue_list %}
{% for course in ue.list|dictsort:"code" %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endblock %}
Thanks!

Using autopagination in django and formatting issues

I'm using django-paginate and getting weird formatting issues with the {% paginate %} tag. I have attached an image of the problem.
I was just wondering what could be potentially causing this?
In the image below I'm on the first page. Notice that the 1 is cut off and also that the pages are strangely ordered and the previous/next is not really visible.
My template is just this for now:
{% extends "base.html" %}
{% load mptt_tags %}
{% load pagination_tags %}
{% load i18n %}
{% block body %}
{% autopaginate parts 20 %}
{% paginate %}
That's not related to Django, neither to Django-Paginate. It seems that you're using Bootstrap as your front-end framework, and that implies issues such that.
I've implemented a similar approach for this site manoomit.com, creating a custom template for managing pagination within django-paginate.
It looks like that:
{% if is_paginated %}
{% load i18n %}
<div class="pagination pagination-centered">
<ul>
{% if page_obj.has_previous %}
<li>‹‹ {% trans "previous" %}</li>
{% else %}
<li class="disabled">‹‹ {% trans "previous" %}</li>
{% endif %}
{% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<li class="active">{{ page }}</li>
{% else %}
<li>{{ page }}</li>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>{% trans "next" %} ››</li>
{% else %}
<li class="disabled">{% trans "next" %} ››</li>
{% endif %}
</ul>
</div>
{% endif %}