Django template if statement always evaluates to true - django

This seems like it should be pretty straightforward, but for some reason I am unable to solve this problem. I'm using Django 1.4. I am trying to do a basic check to see if a list QuerySet is empty or not during template rendering, but the if statement I'm using seems always to evaluate to true.
I have a Django template that reads:
{% extends 'includes/base.html' %}
{% if object_list %}
...
{% block data %}
{% for object in object_list %}
...
{{ object.create_date }}
...
{% endfor %}
{% endblock data %}
...
{% endif %}
'base.html' has the block:
<body>
{% block content %}
...
<div class="row-fluid">
<div class="span12">
{% block data %}
<div align="center"><i>No data.</i></div>
{% endblock data %}
</div><!-- span12 -->
</div><!-- row -->
{% endblock content %}
...
</body>
The view function generating the QuerySet is here:
def barcode_track(request, model):
query = request.GET.get('barcode_search', '')
object_list = model.objects.all()
if query:
object_list = model.objects.filter(barcode__icontains=query)
return render_to_response('barcode_track/barcode_list.html',
{'object_list': object_list, 'query': query},
context_instance=RequestContext(request))
Which is called via this form:
<form id="barcode_search_form" method="get" action="" class="form">
<input type="text" name="barcode_search" value="{{ query }}" />
<button type="submit" class="btn">Search</button>
</form>
And the urls.py line:
urlpatterns = patterns('barcode_track.views',
url(r'^$', 'barcode_track', {'model': Barcode},
name="barcode_track"),)
The idea is that results will only be presented if they exist in object_list, and otherwise the parent block will remain unaltered. I have tried changing the name of object_list, and I have printed {{ dicts }} to the page to ensure that object_list is, in fact, empty (which it is). I am not using a generic view, although I realize that the name suggests as much. I have actually had this trouble in a different app I wrote using similar logic, so I must be doing something systematically incorrectly.
What am I missing here?

You can't wrap control flow tags like if around a block. Your problem is that the child template's definition for block data is being used simply because it's there.
You can fix it by placing the if tag inside block data. If you want to inherit the parent's contents when the list is empty, add an else case that expands to {{ block.super }}.

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

Access StructBlock in a StreamField of a Page.get_children in Wagtail

I try to render a StreamField of a child page in a Page. I don't manage to render the different StructField within the StreamField. Here is my code
class DefinitionPage(Page):
body = StreamField([
('definition', blocks.StructBlock([
('heading', blocks.CharBlock(label='Titre')),
('paragraph', blocks.RichTextBlock(label='Paragraphe')),
]))
])
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
my template. (DefinitionPage is a child of this page.)
{% for post in page.get_children %}
<h2>{{ post.title }}</h2>
{% for block in post.body %}
{% include_block block %}
{% endfor %}
{% endfor %}
post.title is ok but it's like there is no block in post.body.
I tried so many things and {% include_block block %} is certainly wrong. I also tried to add a custom template for the StructBlock without success.
How can I do ? I am using Django 2.0 and wagtail 2.0 (I'm new to wagtail but I read the doc)
Best regards
You need to use page.get_children.specific - get_children only returns the basic Page information common to all page types, which doesn't include the body field in DefinitionPage.
Thank you I changed my code
In the parent PageModel I added:
def get_context(self, request):
context = super().get_context(request)
definitions = self.get_children().specific()
context['definitions'] = definitions
return context
And now in my template:
{% for definition in definitions %}
{% for block in definition.body %}
{% include_block block %}
{% endfor %}
{% endfor %}
I also created a custom template for my definition (simple it's just en test):
{% load wagtailcore_tags %}
<div class="definition">
<h2>{{ value.bound_blocks.heading }}</h2>
{{ value.bound_blocks.paragraph }}
</div>
Thanks a lot
Just last questions: Is there a better practice ? .specific in template or in a custom get_context() ? Is it a good practice to add custom template to StructBlock ?

NoReverseMatch when rendering page

I seem to know where the issue is located since I can get around it, but for getting around it I have to sacrifice a function I really want to keep.
Here is the relevant code in the non-working state:
{% if sections %}
{% for item in sections %}
<a class="sections" href="{% url 'sections:generate' item.section.slug %}">{{ item.section.title }}</a>
{% for subsection in item.subsections %}
<p>{{ subsection.title }}</p>
{% endfor %}
{% endfor %}
{% else %}
<p>Error retrieving sections or no sections found</p>
{% endif %}
The problem part above is in the link tag. Let me explain by showing the related view.py:
def index(request):
sections = Section.objects.all()
context = {
'sections': [],
}
for section in sections:
context.get("sections").append(
{
'section': section,
'subsections': get_subsections(section),
}
)
return render(request=request, template_name='index.html', context=context)
So, 'sections' is an iterable list of items, containing for every items a dictionary with two entries. One, 'section' and one 'subsection'. There are multiple subsections for every section, this is what I really want to accomplish.
Ordinarily, when not bothering with subsections and simply iterating over a list of sections works fine. The template code for that would look something like:
{% for section in sections %}
{{ section.title }}
{% endfor %}
NOTE! The code above works just fine! But as soon as I add 'sections' as a list of dictionaries and have to reference the slug by item.section.slug the pages stop rendering.
Please advise.
Try using tuples:
View:
context['sections'] = [(section, tuple(get_subsections(section))) for section in sections]
Template:
{% for section, subsections in sections %}
<a class="sections" href="{% url 'sections:generate' section.slug %}">{{ section.title }}</a>
{% for subsection in subsections %}
<p>{{ subsection.title }}</p>
{% endfor %}
{% endfor %}

Django 1.6 - parse template tags after import/parse from view

Firstly, I'm relatively new to Django, so forgive me if there's some fundamental misunderstanding on my part.
I'm looking to parse some Django template tags that are imported as a part of a Django render. The end issue I'm seeing is the HTML/JS portions are being correctly called and displayed, but the last Django tags are being rendered as plain text on the page. I realize it's because I'm rendering the view before one step too early, but I'm not sure how to go about this correctly.
Here's the basic call stack (simplified):
template1
{% extends base.html %}
{% block main_content %}
{{ block.super }}
<div id="col1" class="chart-col" style="float: left; width: 33%;">
{% block col1 %}
{% endblock col1%}
{% endblock main_content %}
template2:
{% extends template1.html %}
{% block main_content %}
{% for DATA in DATALIST %}
{{ DATA|safe }}
{% endfor %}
{% endblock main_content %}
python code that generates desired Django / HTML for the above DATALIST in template2:
def chartdisplay(block_var, othervars):
text = "{% block block_var %} \n {{ block.super }} \n"
text += "<html to generate data display goes here>"
text += "{% endblock block_var %}
return text
the above python is passed to the Django View.py:
def dataview(request):
datapull = model.object.filter(**kwargs) #generic data pull
#date-time handler
dthandler = lambda obj: (
obj.isoformat()
if isinstance(obj, datetime.datetime)
or isinstance(obj, datetime.date)
else obj)
data = {'data': datapull}
chart_html = [
chartdisplay(block_var="col1", other_unique_vars),
chartdisplay(block_var="col2", other_unique_vars),
chartdisplay(block_var="col2", other_unique_vars),
chartdisplay(block_var="col3", other_unique_vars)
]
context = {'data', jsondump(data, default=dthanlder), 'charts': charts_html)
return render(request, 'path/to/template2.html', context)
Now, the main issue is the resulting HTML that is displayed displays the HTML and JS fine, but has the Django template tags exposed as text and does not direct the chartdisplay into the desired divs. E.g. template2.html is viewed as:
{% block col1 %} {{ block.super }}
<html/D3 chart is displayed fine>
{% endblock col1 %}
{% block col2 %} {{ block.super }}
<html/D3 chart is displayed fine>
{% endblock col2 %}
{% block col2 %} {{ block.super }}
<html/D3 chart is displayed fine>
{% endblock col2 %}
{% block col3 %} {{ block.super }}
<html/D3 chart is displayed fine>
{% endblock col3 %}
I've tried a few various band-aid fixes but I'd rather just understand how to do this properly. The one thing I've tried but am probably misunderstanding is, before the return render step:
def dataview(request):
...
rendered = render_to_string('path/to/template12.html', context, context_instance=RequestContext(request))
return render(request, 'path/to/template2.html'/, rendered)
Thanks for you help. Please let me know if any clarification is needed!
You don't have to use render, it's just a shortcut that takes a request, template name and context, and returns an http response with the rendered context.
That's not what you want in this case. You have a template string rendered that you wish to render. The low level API to render a template is explained here in the docs.
from django.template import Context, Template
from django.http import HttpResponse
def dataview(request):
...
rendered = render_to_string('path/to/template2.html', context)
template = Template(rendered)
return HttpResponse(template.render(Context(context)))
You may have to use the verbatim tag so that your blocks and extends tags are not evaluated when you call render_to_string on the first template. For example
{% verbatim %}
{% extends template1.html %}
{% block main_content %}
{% endverbatim %}
{% for DATA in DATALIST %}
{{ DATA|safe }}
{% endfor %}
{% verbatim %}
{% endblock main_content %}
{% endverbatim %}

Django: How to get selected text of DropDown in Template?

In the template I get the whole DropDown correctly shown with something like this:
{{form.deal_type}}
But what if I wanted just the text of the selected dropdown shown?
This shows me just a foreignkey.
{{form.deal_type.value}}
I don't know why you want to do this exactly, but try this.
TO LOOP:
{% for value, text in form.deal_type.field.choices %}
{{ value }}: {{ text }}
{% if value == form.deal_type.value %}
<strong>{{ text }}</strong> <!-- THIS IS THE SELECTED ONE... -->
{% endif %}
{% endfor %}
EDIT:
I meant the above code as an illustration, not that you should use it verbatim. This code will do more like what you want.
{{ form.deal_type.label_tag }}
{% for value, text in form.deal_type.field.choices %}
{% if value == form.deal_type.value %}
{{ text }}
<input type="hidden" name="deal_type" value="{{ value }}" />
{% endif %}
{% endfor %}
I had a similar issue. To solve it I just passed the value to the template directly from the view. So in your view you presumably have something in the way of
data = {'form' :form,}
return render_to_response('destination.html', data, context_instance = RequestContext)
In data you are passing the form that includes deal_type. Add to
data a variable deal_type set equal to Object.deal_type.display_value with
data = {'form' :form,}
if Object.deal_type: data['deal_type'] = Object.deal_type.display_value
return render_to_response('destination.html', data, context_instance = RequestContext)
Then on your template you can just use
{% if condition_to_show_just_text %}
{{deal_type}} {{form.deal_type.as_hidden}}
{% else %}
{{form.deal_type}}
{% endif %}
It may be insiginificant in this case, but it seemed to me that if the list was long, iterating with the for loop on the template would be less efficient than pulling directly from the object