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.
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
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/
I have a form in Django with radio buttons that have various properties for users. When an admin user (in my application...not the django admin) wants to edit properties for different users, the same form objects should appear for all users in a list. So say there is a list of possible permissions for users, I have a table with the list of users, and the user permissions options (radio buttons) should appear next to them. In the form I have a radio button choice field with all available permissions. But in the view, how do I get it to appear for each user? Also, how do I set initial data when there're are many of the same form object? Is there an example of this somewhere? Thanks!
[EDIT]
Below is basically what I am trying to show:
<table class="admin_table">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>User Permission</th>
</tr>
{% if users|length > 0 %}
{% for user in users %}
<tr>
<td>{{ user.first_name }}</td>
<td>{{ user.last_name }}</td>
<td>[RADIO BUTTONS]</td>
</tr>
{% endfor %}
{% endif %}
</table>
The place where I wrote [RADIO BUTTONS], is where I woud like to show multiple radio buttons which should appear for each user, not for the whole form.
Also, I'm looking at this site: http://jacobian.org/writing/dynamic-form-generation/. Not sure if that's the solution, but the problem the author is trying to solve is very interesting anyway.
Maybe you can try something like this:
The key is to group the radio button inputs for each user using {{user.id}}. You can name them as you want.
{% for user in users %}
<tr>
<td>{{ user.first_name }}</td>
<td>{{ user.last_name }}</td>
<td><input type="radio" name="user_{{user.id}}" value="Radio1" >Radio1</input></td>
</tr>
{% endfor %}