Is there a way to access a specific form from an inline formset, in the template, without hardcoding the index?
I know that the usual way to iterate through a formset is to do something like:
{% for form in formset %}
{{ form }}
{% endfor %}
But due to some details on the template (i have multiple formsets, that should be displayed side by side on a table, inside another for), it would be better if i could access each form by its index. I can do this by hardcoding the index, like {{ formset.0 }}, but since i'm iterating in the template, the ideal would be to get the form by the forloop.counter, so that i could do something like
{% for field in fields %}
<tr>
<td>{{ field }}</td>
<td>{{ formset1.[forloop.counter0] }}</td>
<td>{{ formset2.[forloop.counter0] }}</td>
</tr>
{% endfor %}
Is there a way to achive this?
Custom indexing isn't possible inside template.
You can achieve the same result by creating your own filter. See the following snippet:
http://djangosnippets.org/snippets/2740/
Related
I have queries like following that I would like to implement it in front end:
MembershipPayment.objects.filter(group__name=tg.group_name).are_valid().count()
MembershipPayment.objects.filter(group__name=tg.group_name).Not_valid().count()
I know that I can pass this from view to front-end HTML, but the problem is that in the front end I have a query-set containing many "group"s. so I need to run a similar query for each of those "group"s.
I need something along these lines [this code of course won't work] in front:
{% for rec in groups %}
<tr>
<td>{{ MembershipPayment.objects.filter(group__name=tg.group_name).are_valid.count }}</td>
<td>{{ MembershipPayment.objects.filter(group__name=tg.group_name).not_valid.count }}</td>
</tr>
{% endfor %}
So I was wondering how can I achieve this without changing my model structure (if possible).
Why don't you do that logic in your view, by building a dict for each of your group, like:
def your_view(request):
...
groups: dict = dict()
for group_name in group_names:
groups[group_name]: int = MembershipPayment.objects.filter(group__name=group_name).are_valid().count()
return render(request,'your_template.html', {'groups': groups})
..and then pass it to your template, like:
{% for key, value in groups.items %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
...key being the group name, value being the group count.
I ended up passing a dictionary like this in view:
groups_statistics={group1:{monthly_balance:250,weekly_balance:3000},group2:{monthly_balance:250,weekly_balance:3000}}
and then looping through its value as:
{% for rec,stat_dic in groups_statistics.items %}
<tr>
<td>{{ stat_dic.weekly_balance| get_value_from_dict:rec }}</td>
<td>{{ stat_dic.monthly_balance| get_value_from_dict:rec }}</td>
</tr>
{% endfor %}
here get_value_from_dict is a customized tag defined in dictionarytags.py as:
from django import template
register = template.Library()
#register.filter('get_value_from_dict')
def get_value_from_dict(dict_data, key):
"""
usage example {{ your_dict|get_value_from_dict:your_key }}
"""
if key:
return dict_data.get(key)
in the following folder:
my app>"templatetag" folder.
this folder contains:
__ init __.py
and
dictionarytags.py
and then in html I have this line in upper part:
{% load dictionarytags %}
I'm trying to build a table in a template based on a variable number of fields.
The code I'm using is:
<table id="custom_report_table" class="display" width="100%">
<thead>
{% for field in fields %}
<th>{{ field }}</th>
{% endfor %}
</thead>
<tdody>
{% for CI in CIs %}
<tr>
<td>{{ CI }}</td>
</tr>
{% endfor %}
</tdody>
</table>
fields is a list with all the fields and CIs is a queryset with the data that needs to go into the table.
The problem is that I usually know the name of the fields so I can call each on individually when creating the cells in the usual way:
{{CI.field1}}
{{CI.field2}}
....
But now I can't hard code the fields' names as they are variable and come from the list.
Is there a way to do this?
Thanks,
Isaac
Just iterate again over CIs using items
{% for key,value in CIs.items %}
<td>{{ key }} {{value}}</td>
{%endof%}
If you only want to print items that are in fields:
{% for field_name, field_value in CIs.items %}
{% if field_name in fields %}
<th>{{ field_name }}</th>
<td>{{ field_value }}</td>
{% endif %}
{%endof%}
Solved by using .values in the queryset creation in the view.
And to reference the foreign keys for each field I had to build up the list of values with a list of field_name_foreign_field.
As the names for all foreign key fields followed a standard rule, it was quite easy with a for loop in the view.
i need some help in following situation.
After a successful searching for a user, i want to show the users information. It's a m2m flied, but I'm getting all objects from this model.
I do not know, how to filter users information.
Got this template:
{% for player in players %}
<tr>
<td>{{ player.last_name }} <span class="text-muted">({{ player.first_name }})</span></td>
<td>{{ player.gender }}</td>
<td>
{% for choice in search.league %}
<div class="">
{{ choice }}
</div>
{% endfor %}
</td>
This way it is showing all objects in League.
The field league is a M2M field from player.
I have this in my forms.Form
league = forms.ModelMultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, queryset=League.objects.all())
I realize that I must also send the users-information from the view, but I do not know how.
Thanks for helping.
I would say that you want to use something like {% for choice in player.league.all %} in your template and use it in your for-loop. You can find more information about this here: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.related_name
In the template below, user.group is a number and it has to be shown as group_name.get(user.group) Are there any ability to pass to template group_name dict and use group_name.get(user.group) inside template?
<table>
{% for user in users %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
<td>{{ user.group}}</td>
</tr>
{% endfor %}
</table>
In the spirit of django this logic should live in the code, not in the template. Can't you add a method user.get_group() that returns the group?
No, the dot notation doesn't allow for looking up names by variable value.
I would do this in the view, if it's specific to this one template.
for user in users:
users.group_name = group_name.get(user.group)
{% for user in users %}
{{ user.group_name }}
{% endfor %}
If it's applicable to all users, do as jammon suggested and define a method on User
Out of curiosity, can I see your models? Just curious why group is a number and where group_name comes from.
Is it possible to render each checkbox individually, instead of it having to clump all the checkboxes together in a list, as it does by default? Something like
{{ myform.cbmultiple.0 }}
To render just the first checkbox? Actually the 0 would have to be a variable so I can loop...
The reason I'm asking is because I want to display these checkboxes in a rather complicated way, so the default widget doesn't work for me. I also don't really want to override the widget because it's much easier to render it using the template syntax than in python code, plus, that's a lot of work for a one-off.
No you can't do that because the whole HTML is generated by the widget's render method at once. You could only create your own widget class and override the render method so that it produces the HTML in a fashion that suits your purpose!
There is a way to render the CheckboxSelectMultiple() manually in the template so you can do what you want with it.
Take a look at this post for details.
The solution could be something like this:
<table>
<thead>
<tr>
<td> </td>
<td>V</td>
<td>S</td>
</tr>
</thead>
{% for pk, choice in form.options.field.widget.choices %}
<tr>
<td>{{ choice }}</td>
<td><label for="id_options_{{ forloop.counter0 }}"><input {% for option in app.options.all %}{% if option.pk == pk %}checked="checked"{% endif %}{% endfor %} type="checkbox" id="id_options_{{ forloop.counter0 }}" value="{{ pk }}" name="options" /></label></td>
</tr>
{% endfor %}
</table>