Split Django Queryset in template for Bootstrap Carousel - django

I have a model Banner with fields image and name. I need data from this model to be displayed in groups of three on a moving Bootstrap carousel.
My current implementation is the most expensive and simple one I could think of.
banner1 = Banners.objects.filter(id__exact=1) repeated
for 9 entries in the Model.
My question is that is it possible to split one queryset
Banners.objects.all() into the three groups of three entries and then how would I go about displaying the three groups across three different slides of the Bootstrap Carousel?

This seems like a good place to use the django divisibleby filter. Since you need to add the extra grouping info every three instances, it could look something like this (very abstractly, since I don't know your specific template demands):
{% for banner in banners %}
{% if forloop.counter0|divisibleby:"3" %}
<!-- your carousel wrapping code here -->
{% endif %}
<!-- code for each individual banners here -->
{% if forloop.counter0|divisibleby:"3" %}
<!-- rest of your carousel wrapping code here -->
{% endif %}
where banners would be a context variable that contains Banners.objects.all()

Related

Django nested templates

In trying to keep with DRY, I'm setting up my Django project HTML files now. I have successfully extracted most repeated information to my base.html page. On most (but not all) of my pages, my content is displayed within a general 'panel' which is basically just a container set-up with styling, but it's got a few div-tags to it so it looks a bit ugly and I'm having to type out the exact same code out several times on each page.
My idea was to extract this to a 'panel.html' then call it whenever I need it, for example some pages might just have one 'panel' whereas my dashboard (it's an administrative site) will have maybe 15+. So it seemed a better idea and cleaner to not have to type out all this code each time I need to set up a 'panel'.
My ideal page would look something like..
{% extends 'base.html' %}
{% block content %}
{% extends 'panel.html' %}
{% block panel_content %}
Panel content...
{% endblock panel_content %}
{% extends 'panel.html' %}
{% block panel_content %}
Second panel content
{% endblock panel_content %}
{% endblock content %}
I know I can't use extends multiple times but I'm using it just as an example for what it is I'm trying to achieve.
I am going to potentially have hundreds of these identical 'panels' across my site but each containing different content and it would be so much cleaner if I could just have one stored somewhere in a HTML file and call it however many times I need.
Is there a way to do this?
You can use include
{% include "panel.html" %}
I should mention that too many include statements create a performance issue.

How do I check if queryset is empty for multiple objects in django?

So, my template is receiving an object that has generic foreign key relations with 3 other models and now I need to check if all three of these exist or not to show the user a custom message based on what exists and doesn't exist.
Here's some perspective:
Person Object has 3 different models attached to it:
Address
Email
Phone
If all 3 don't exist, then I want the template to say
`No contact details found, you may add new contact info"
But, even if either of the three exist, then don't show the message. I tried this:
{% if person.address.all and person.email.all and person.phone.all %}
<!-- Do something here to show the details of each object -->
{% else %}
<!-- Show the default message -->
{% endif %}
But what happens here is that if Address exists, but Phone and Email do not, it'll show the message - which is not what I want. How do I achieve my desired result? Can I check for Null?
I recommend creating a property and using it in the template, to avoid placing so much logic in the template:
In the model:
class Person(models.Model):
# ...
#property
def has_contact_details(self):
return self.address.exists() or self.email.exists() or person.phone.exists()
In the template:
{% if person.has_contact_details %}
<!-- Do something here to show the details of each object -->
{% else %}
<!-- Show the default message -->
{% endif %}
Use 'or' instead of 'and'. If you use 'and', it will return true only when each of email&phone&address have at least one record.
Please refer the link for better understanding 'and' and 'or'.
The Peculiar Nature of and and or
{% if person.address.all or person.email.all or person.phone.all %}
<!-- Do something here to show the details of each object -->
{% else %}
<!-- Show the default message -->
{% endif %}

Best practice for Django components

Django provides a means of collecting assets into its Widget framework, and in turn using a form to present it, but how about doing this outside the forms framework?
For example, let's say I just want a simple, self-enclosed web component that requires two .js files, a CSS file, a template, and is backed by a view to generate some data. This isn't a "form" because it's not intended to collect input. Rather, assume it's, say, a customized scrollbar. I can't seem to find the right match for this usage pattern even though it seems common and straight-forward.
To do this now seems to mean adding the CSS to the top-level template; the JS to the template (or script tags arbitrarily in the included document); and then rendering the form as an include. But wouldn't it be easier to have something like:
class ScrollBar(...):
template_name = ...
class Assets:
...
and then rendering it via a templatetag, but with the ability for a context processor or other hook to extract the assets into their appropriate places in the page?
Something like:
index.html:
<html>
<head>
...
{% compressed_css %}
</head>
<body>
...
{% compressed_js %}
{% block content %}
{% endblock %}
</body>
scrollbar-using-page.html
{% extends "index" %}
{% block content %}
{% scrollbar args %}
{% endblock %}
with the end result being the scrollbar's template is rendered with the specified args, and its JS and CSS are merged into the compressed_css and compressed_js areas, respectively, above?
I think we get there almost 100% via this approach, without the compressed_js/compressed_css sections, which raises the question of whether then we just included these in all shared pages. (Alternatively, we could use nested/inherited blocks.)
Adam

Adding an if statement to django-cms template tag

I'm probably missing something very obvious but can't see anything in the documentation. I have a carousel with a and each will hold an image. However I've added 6 but I want to add an if statement so if an Image has not been added you don't see a blank space, where there is no content inside the .
Here is what i've tried so far:
{% if "Carousel 1" %}
<li>
{% placeholder "Carousel 1" %}
</li>
{% endif %}
Attempt 2:
{% placeholder "Carousel 1" as cara1 %}
{% if cara1 %}
<li>
{{ cara1 }}
</li>
{% endif %}
Not sure if there is something differnt i need to be doing for the django-cms template tags?
Any help would be much appreciated. Docs here - http://docs.django-cms.org/en/latest/advanced/templatetags.html#placeholder
Not to be rude, but your approach is way, way off :)
Placeholders hold Content Plugins. Content Plugins are responsible for how they render their contents.
My advice would be to create or find a carousel content type plugin. This plugin will hold multiple images or "CarouselImage" model instances that you can iterate over, and also specify a template with which to render itself.
In this template resides the conditional statement you're wanting to check for. Placeholders are just that - places held for content plugins.

How to organize checkboxes in several columns in Django forms

I am a newbie to Django and web-development in general so be patient for maybe a very dumb question :)
I have a form generated from a model and in this form I have about 20 checkboxes. Now they are aligned in one long column and it looks not very nice from a UI point of view. I'd like this column to be split in several ones but still have this form be automatically generated from the model. What would you suggest me to do?
In the generated HTML individual checkboxes look like this:
<li><label for="id_boxes_0"><input type="checkbox" name="boxes" value="1" id="id_boxes_0" /> some name</label></li>
You don't need to change anything in Python code, but you'll need to layout the form in the template instead of using {{ form.as_ul }}. You can iterate over the form to get the fields. For the simplest possible approach, something like the following could put twenty fields in two columns of ten:
{% for field in form %}
{% ifequal forloop.counter 11 %}</ul><ul>{% endifequal %}
<li>{{ field }}</li>
{% endfor %}
Personally I never use the as_* helper methods in real code, as far as I'm concerned they're only useful for rough prototyping.