django radio buttons in forms - django

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

Related

How can I add value in Django 2 template

Scenario:I want to make a page for available slots for booking ,for which I want print time slot for 1 hour that is from i To i+1 and color it according to the availability.
I am newbie to django and can't figure out the way to make a calendar and that is why I am printing time values in HTML template.
Is there any other way to make this page and also is printing 'i+1' possible.
Views.py
#login_required
def slots(request):
k={ 'pro':range(8,17)
}
return render(request, 'login/slots.html',k)
slots.html
{% for i in pro %}
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td id="demo">{{ i }} to {{ i+1 }}</td>
</tr>
{% endfor %}
I know this might be a silly question but I am not able to figure it out,
any help is appreciated.
There is a built-in filter in Django template called add. You can add something to a value in templates using {{ value|add:"2" }}.
In this case specifically, try:
<td id="demo">{{ i }} to {{ i|add:"1" }}</td>

Django Simple History - History Diffing

So I'm using django-simple-history for one of my projects. I'm using it on one of the models called "Address" to show the history of the records.
I've created a DetailView to show the information about the address and added context['history'] to show changes of the record. This works all fine.
I would be interested in what field changed, and I read the following; History Diffing
So I somehow need to loop through all the fields from the last two records and find the field which has changed...
I couldn't find any examples on how to achieve this so I tried adding the context to the view
#views.py
class Address(DetailView):
'''
Show details about the address
'''
model = Address
'''
Add history context to the view and show latest changed field
'''
def get_context_data(self, **kwargs):
context = super(Address, self).get_context_data(**kwargs)
qry = Address.history.filter(id=self.kwargs['pk'])
new_record = qry.first()
old_record = qry.first().prev_record
context['history'] = qry
context['history_delta'] = new_record.diff_against(old_record)
return context
And a simple model
#models.py
class Address(models.Model)
name = models.CharField(max_length=200)
street = models.CharField(max_length=200)
street_number = models.CharField(max_length=4)
city = models.CharField(max_length=200)
Template
#address_detail.html
<table>
<thead>
<tr>
<th scope="col">Timestamp</th>
<th scope="col">Note</th>
<th scope="col">Edited by</th>
</tr>
</thead>
<tbody>
{% for history in history %}
<tr>
<td>{{ history.history_date }}</td>
<td>{{ history.history_type }}</td>
<td>{{ history.history_user.first_name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Somehow this doesn't feel right, there should be a way to iterate over the changes and only add the changed fields to the context...
Any ideas would be appreciated!
I have recently worked on this. I think you are missing a trick you have changes stored in history_delta. You can use this to show the changed fields.
Following will display tabulated result like which field was changed and what was the old and the new value of that field.
{% if history_delta %}
<h3>Following changes occurred:</h3>
<table>
<tr>
<th>Field</th>
<th>New</th>
<th>Old</th>
</tr>
{% for change in delta.changes %}
<tr>
<td>
<b>{{ change.field }}</b>
</td>
<td>
<b>{{ change.new }}</b>
</td>
<td>
<b>{{ change.old }}</b>
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No recent changes found.</p>
{% endif %}

django template variable number of fields build of table from queryset

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.

Filtering user and showing his informations- django

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

Using complex variables in django templates

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.