Setting ID for Radio buttons in Django - django

It is great that Django 1.4 allows fine graining of radio select
{% for radio in form.important_client reversed%}
{{radio.tag}}<label for="????">{{radio.choice_label}}</label>
{% endfor %}
but for some odd reason when using this methodology, the <input> have no IDs. And hence I can't set the <label for='ID' /> accordingly. That causes big issues in my CSS.
Is there anyway to get the IDs set nonetheless?

While debuging a RadioSelect rendering, I got no idea of using radio tag and label elegantly. So here is my attempt to solve your problem:
{% for radio in form.important_client reversed %}
<input name="{{ radio.name }}" type="radio" id="radio_{{ radio.index }}" value={{ radio.choice_value }}>
<label for="radio_{{ radio.index }}">{{ radio.choice_label }}</label>
{% endfor %}
Instead of radio.index property, which is not documented, you can use forloop.counter.
Just in case I attach a screenshot of debug window, where example of radio context is shown (form_of_field variable on a figure):

This is one way to do that, it might not be the best but it works. In your form you can set the id for each of the choices like this:
from django import forms
class MyForm(forms.Form):
CHOICES = (('1','Available'),('2','Not Available'))
input = forms.ChoiceField(widget=RadioSelect(attrs={'id' : 'myId'},choices=CHOICES)
Then on your template:
{% for radio in form.input %}
{{ radio }}
{% endfor %}
And your HTML will look like this:
<label for="myId_0"><input id="myId_0" name="input" type="radio" value="1"></label> Available
<label for="myId_0"><input id="myId_0" name="input" type="radio" value="2"></label> Not Available
I hope this works!

Related

trouble showing desired checkbox validation state w/ bootstrap5 for django model form w/ m2m field and checkboxselectmultiple widget

I have a checkboxselectmultiple on an m2m model field in an ModelForm that is required - meaning at least one of the choices must be selected. I am using the boostrap5 was-validated class on my form:
<form method="POST" action="{{ request.path }}" {% if attempt_submit %}class="was-validated"{% endif %}>
This question is about how the validation shows up on my form with bootstrap5. Should be red border and red ! if not validated, green border and checkmark if so. However, for my checkboxes, if I don't have any selected (and everything else on the form validates), the form will show each checkbox option as green instead of red. Yet, it does know that it's invalid because the page focus will come back up the checkbox area to show the user what to correct (and it doesn't pass form.is_valid() in views.py.
Why are these labels and boxes still showing green and how can I show them as red until I select one and it's now valid?
Along the lines of this post, I have tried adding
{% if form.sales_location.field.required %}required{% else %}form.sales_location.field.required=""{% endif %}
to the checkbox <input>, but then each field is required and if I select one, the other remaining options still remain red - as if every option would have to be selected for the form to validate. Am I supposed to do this anyway and then add something else (JS?) to disable that?
Not sure exactly what code would be helpful to see...
in models.py, this is the field:
sales_location = models.ManyToManyField(SalesLocation, verbose_name="Where do you sell your products? (select all that apply)" )
in forms.py
model = AssessmentProfile
fields = [
'sales_location',
...
]
widgets = {
'sales_location': forms.CheckboxSelectMultiple(attrs={
'class': 'form-check',}),
}
I add this because I read this post about making sure that I use a `ModelMultipleChoiceField' - but I assume that is already happening because it's a model form.(?)
Probably most important, in the template thisform.html, here's how I'm manually adding this form element:
<div class="field-wrapper">
{{ form.sales_location.label_tag }}
<ul id="id_sales_location" class="form-check">
{% for pk, choice in form.sales_location.field.widget.choices %}
<li>
<input {% for location in location_qs %}{% if location == pk %}checked='checked'{% endif %}{% endfor %}
name="sales_location" class="form-check-input" type="checkbox" value="{{ pk }}" id="id_sales_location_{{forloop.counter0}}"
{% if already_submitted %}disabled="disabled"{% endif %}>
<label class="form-check-label" for="id_sales_location_{{forloop.counter0}}">
{{ choice }}
</label>
</li>
{% endfor %}
</ul>
</div>
Also, I tried updating css to manually format red, but think that doesn't address the root of the problem, plus, I wasn't able to do it successfully anyway.
Thanks for taking a look and for any suggestions.
In the end, I used javascript to solve this problem.
I updated the form template
<div class="field-wrapper">
{{ form.sales_location.label_tag }}
<ul id="id_sales_location" class="form-check">
{% for pk, choice in form.sales_location.field.widget.choices %}
<li>
<input {% for location in location_qs %}{% if location == pk %}checked='checked'{% endif %}{% endfor %}
name="sales_location" class="form-check-input" type="checkbox" value="{{ pk }}" id="id_sales_location_{{forloop.counter0}}"
{% if not form.sales_location.field.required %} {% else %} required {% endif %}
{% if already_submitted %}disabled="disabled"{% endif %}>
<label class="form-check-label" for="id_sales_location_{{forloop.counter0}}">
{{ choice }}
</label>
</li>
{% endfor %}
</ul>
</div>
to add required to the input if the checkbox is required. This allows all the checkboxes to come up red when validating, if the field is empty.
Then, I added this javascript to remove 'required' if it's checked.
<script>
// Select all checkboxes using querySelectorAll.
var checkboxes = document.querySelectorAll("input[type=checkbox][name=sales_location]");
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
for (var cb of checkboxes) {
cb.removeAttribute('required');
}
})
});
</script>
If the field is not required, nothing changes. But if it is, then the required attribute on the <input>is gone and all the checkboxes show up green, which is what I wanted.
It's not perfect because if the checkboxes become unchecked, they don't change back to red. So I am making a dirty assumption that if someone checked a box, they wouldn't go back and uncheck it and try to submit. In which case, the validation would show green (and unchecked) until Submit was pressed again, but then it would take them back to this field which would be red again. If you know how to improve my code by adding the different case for the change function (only if the field is required), please feel free to add that. Cheers.

Django checkbox for filter

Im having some problem when I want to keep a checkbox checked. When I send the form all the checkbox are checked. so idk how to change the if statement for that :(
<div class="form-group">
<label >Marca</label>
{% for brand in q %}
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="{{brand.brand}}" name="test" value="{{brand.brand}}" {% if marca %} checked="checked" {%endif%}>
<label class="custom-control-label" for="{{brand.brand}}" style="cursor: pointer;">{{brand.brand}}</label>
</div>
{% endfor %}
</div>
And here is the view:
marca = request.GET.get('test')
if marca :
products = products.filter(brand__name__in=request.GET.getlist('test'))
All the other things are fine. It shows me the brands that I choose. So I just want to keep the checkbox that I checked :( and I think the problem is that If statement in the template
Just pass a set of the values in the values to the template:
marca_vals = set(request.GET.getlist('test'))
# …
context = {
'marca_vals': marca_vals,
# …
}
return render(request, 'some_template.html', context)
in the template you can then render it with checked in case the value is in the marca_vals:
<input type="checkbox" {% if brand.brand in marca_vals %}checked{% endif %} class="custom-control-input" id="{{brand.brand}}" name="test" value="{{brand.brand}}">
You might however want to consider using a form, or even django-filters [readthedocs] to both make filtering and rendering the form more convenient.

How I can get object attributes for different language codes in Django template?

I am using django-transmeta for translation. In the below code, {{ obj.description }} returns the description in the current language of django. What I need is, getting the obj.description_[lang_code]. How can I get it?
{% for lang in languages.all %}
<div id='{{ lang.code }}'>
<input type="text" name="description-{{lang.code}}" value='{{ obj.description }}'/>
</div>
{% endfor %}
As I understood from your comment you want to get description of specific language in for loop?
then simply write a custom filter like in this way
{{ obj|get_lang_info:lang.code }}
here get_lang_info is custom filter.

Django template get custom attribute from request OR "refresh" radio buttons

I currently have a template containing an html form, with the lines:
{% for r in q1.responseoption_set.all %}
<span class="r"><input type="{{ q1.answer_type }}" name="r{{ r.id }}" id="r{{ forloop.counter }}"/>
<label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ forloop.counter }}">{{ r.text }}</label></span><br>
{% endfor %}
problem is, because they don't all have the same name (that's why, right?), if I pick a radio button, and then switch to another one, the first one still shows as selected.
However, at the moment, I need them to all have different names because I need to be able to identify the choices within my view, and as far as I can tell, all I can get from the request is [name, value], e.g. [r200, "on"]
The only way around this that I can think of is to insert a script that assigns a check event to each button, and then, once checked, inserts a hidden input with the name I want, but that seems messy.
SO, is there a way for me to either:
get the button id from the request OR have the buttons "refresh" somehow as they are.
Keep the name the same, and set the value for each input choice to the answer id.
{% for r in q1.responseoption_set.all %}
<span class="r"><input type="{{ q1.answer_type }}" name="{% questionId %}" value="r{{ r.id }}" id="r{{ forloop.counter }}"/>
<label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ forloop.counter }}">{{ r.text }}</label></span><br>
{% endfor %}

Get the ID of a field widget in a formset

I want to customize the layout of forms in a formset (that is, I don't want to use .as_table() or .as_p() and the like). I'm trying to get the name of a form field for use in its label's for attribute, but I'm not sure how to go about it. I'm hoping that I won't need to construct a new name/ID for the field from scratch. Here's an example of what I'm working with right now:
{% for form in formset.forms %}
<!-- The field for the "path" form field -->
<label for="{{what do I put here?}}">{{form.fields.path.label}}:</label><input type="text" id="{{django creates this one; do I have to do my own with the for loop counter or something?}}" name="{{probably the same as id}}" />
{% endfor %}
Is there any sort of "create ID for formset field" sort of method?
This is likely what you want.
for="{{ form.your_field.html_name }}"
First, you want to use the form element's id, instead of name.
I tried Django 1.3 Alpha-1 and the following worked:
{% for form in formset.forms %}
<label for="{{ form.my_field.auto_id }}">{{ form.my_field.label }}</label>
{{ form.my_field }}
{% endfor %}
Enjoy!