Django regroup grouper - python

I have successfully implemented the regroup call in my template to order missed donations by a pickup_id. The goal was to display all the routes where a a missed donation occured, and a list of all the names underneath the route name. Pickup routes can have the same name so I was grouping by pickup_id. When I do that and call {{ route.grouper }} it returns the ID of the pickup of course. How can I call the field 'route' which displays the route name from grouper?
I was trying things like this...
{{ route.grouper.route }}
{{ route.route.grouper }}
view
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_id')
template
{% regroup missed_routes by pickup_id as missed_pickups %}
{% for route in missed_pickups %}
<p>{{ route.grouper }}</p>
<ul>
{% for donor in route.list %}
<li>{{ donor.last_name }}</li>
{% endfor %}
</ul>
{% endfor %}

Grouper is just a string, so you have to get name from route instance. Not sure it's gonna work, but try {{ route.list.0.pickup.name }} (I assume that pickup is foreign key to Pickup model with name field) instead of {{ route.grouper }}

Related

How to add an attribute to the form tag itself and not just a field in Django?

I am having trouble stopping bots filling in spam while letting through legit users. I have a honeypot field with autocomplete="off" attribute but it doesn't seem to be working. From what i've read, the best cross browser solution is to add autocomplete="false" to the main form tag itself, e.g. <form autocomplete="false">...</form>. What is the best way to do this in Django?
Just do that in your template where the form is added.
In a template you'd typically do something like;
<form autocomplete="false">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.label }}
{{ field }}
{{ field.help_text }}
{% endfor %}
So just add whatever you want to the form tag.
You should probably also have a look at including recaptcha if you've got spam problems.
And remember that v3 doesn't require any selecting street lights etc
https://pypi.org/project/django-recaptcha/

Django - dictionary key and value not displaying in template

I'm trying to build a simple blog with Django and am trying to display blog posts and the number of comments on associated with a post. Unfortunately, I'm running into trouble getting my dictionary to print out values--or, at least where I want to.
In my views.h file:
class IndexView(generic.TemplateView):
template_name = 'blogs/index.html'
num_comments = { }
def get_blogs(self):
"""
Returns the last 5 published blog posts.
"""
blogs = BlogPost.objects.filter(
pub_date__lte = timezone.now()
).order_by('-pub_date')[:5]
for blog in blogs:
# Setting our num_comments dictionary by getting
# the number of comments from a particular blog post
self.num_comments[blog.id] = len(Comment.objects.filter(blog_post = blog.id))
return blogs
In my index.html file:
{{ view.num_comments }}
{% if view.get_blogs %}
{% for blog in view.get_blogs %}
<div>
<h1>{{ blog.post_title }}</h1>
<p>{{ blog.post_text }}</p>
<ul>
{{ blog.id }}
{{ view.num_comments }}
{% for key, value in view.num_comments %}
<li>
{{ key }} <-- Does not display
{{ value }} <-- Does not display
</li>
{% endfor %}
</ul>
</div>
{% endfor %}
{% else %}
<p>No blogs are available.</p>
{% endif %}
Where I explicitly call {{ view.num_comments }}, the correct dictionary is being displayed. Any idea why my dictionary isn't getting the key and value pair correctly? Thanks.
You should use .items() method to access the key and value pairs:
{% for key, value in view.num_comments.items %}
Also see the third example in Django documentation.

accessing a list of tuples in html

I have a list of tuples called top_5 which are the top 5 users usernames and their corresponding post count.
This is a list that has 5 items and each item has 2 indexes.
{% for user in top_5 %}
{{ user }}
{% endfor %}
returns the user and his post count but is there a way I can get them separately?
Is this a Django template? You can access index values in Django template using the dot notation:
{% for user in top_5 %}
{{ user.0 }} {{ user.1 }}
{% endfor %}
And as mentioned in the comments, unpacking the tuples is also an option:
{% for username, post_count in top_5 %}
{{ username }} {{ post_count }}
{% endfor %}

django template print out by filter id value

I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.

django regroup grouper name as choices verbose name

Taking simple regroup example from Django documentation:
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }}
<ul>
{% for item in country.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
If country.grouper is a Char field declared in model with choices=CHOICES_FIELD, how can I display it's verbose name in template ? Normally i would take Model.get_FOO_display but country.grouper.get_country_list_display of course will not work.
Is custom template tag only choice ?
You simply have to look at it the other way around!
Use get_FOO_display as the grouping field.
{% regroup cities by get_country_display as country_list %}
{{ country.grouper }} will now display the value fields from the
choices set rather than the keys.
(taken verbatim from djangodocs)

Categories

Resources