I am trying to get the value for a select option in my django template. I can iterate through an object like this:
<select id="id_name" name="name">
{% for x, y in form.fields.name.choices %}
<option value="{{ x }}">{{ y }}</option>
{% endfor %}
</select>
but is there any way to get a specific value eg; form.fields.name.choices.2? without looping? Thanks!
You can try
form.fields.name.choices.queryset.values.2
to get third element in a dict with your choices. Or you use
form.fields.name.choices.queryset.values_list.2
to get the choice as tuple.
See here https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values and here https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values-list
Related
In my template, I am creating a select control from the list in qualifications. I have added the comment line to try to figure out why I could not designate a given row as selected.
{% for qual in qualifications %}
<!-- form.qualid.value ({{ form.qualid.value }}) {% if form.qualid.value == qual.empqualid %}equals{% else %}does not equal{% endif %} qual.empqualid ({{ qual.empqualid }}) -->
<option value="{{ qual.empqualid }}"{% if qual.empqualid == form.qualid.value %} selected{% endif %}>{{ qual.EmpQualName }}</option>
{% endfor %}
A sample of my results:
<!-- form.qualid.value (166) does not equal qual.empqualid (558) -->
<option value="558">Gardening</option>
<!-- form.qualid.value (166) does not equal qual.empqualid (166) -->
<option value="166">General Manual Labour</option>
<!-- form.qualid.value (166) does not equal qual.empqualid (571) -->
<option value="571">General Manual Labour (Chinese)</option>
The middle row should be a match, as the qual.empqualid is the same as the form.qualid.value (at least it looks like it to me). The variable form.qualid is an IntegerField and qual is a dictionary that has a key 'empqualid' that also holds integers. I think that forms emit strings with value()? Is that a possible issue? If so how do I check or change types?
As you can see, django thinks that 166 and 166 are different in some way. Can somebody tell my how this might happen?
As I suspected, it's a type problem. It turns out that qual.empqualid was somehow not being interpreted as an integer (it is retrieved from an sql database where it is one) and so the form field form.qualid which is an integer was not considered equal.
The fix was to add |floatformat:"0" to the end of qual.empqualid so they are both being interpreted as integers. The select list now works as expected.
This is odd, because in the same template, I have another select field produced the same way which already works as expected with no floatformat filter. It also compares a database sourced field with a form IntegerField, but it works there.
Bit of a mystery.
I am building a dashboard which will have some information about users. I am trying to set value to select tag using Django but the value never get assigned. Here is my code.
My model look something like this
class User(models.Model):
first_name
last_name
reg_status
I need to fill in the template with the reg_status. The reg_status can contain three options, they are as mentioned below:
mailed
register
not register
The value of reg_status is dynamic. For example, if I pass in reg_status = mailed from my view, I need the <option value='mailed'> to be selected. However, if I pass in the reg_status = register from my view, I need the <option value='register'> to be selected.
Here is how I am trying to render the content in the template:
{% for u in user %}
<input type='text' value={{u.first_name}}/>
<input type='text' value={{u.last_name}}/>
<select value={{u.reg_status}>
<option value='mailed'>mailed</option>
<option value='register'>register</option>
<option value='not register'>not register</option>
</select>
{% endfor %}
The output give 'mailed' as selected for all instance even if the value is set to 'registered' or 'not registered'.
You can use a Jinja ternary operator. It's a little greedy because you're doing the same operation 3 times. You could also look into setting a global variable with Jinja (see the documentation, scroll down to Scoping Behavior).
<select value='{{ u.reg_status }}'>
<option value='mailed' {{ "selected='selected'" if u.reg_status == 'mailed' else "" }}>mailed</option>
<option value='register' {{ "selected='selected'" if u.reg_status == 'register' else "" }}>register</option>
<option value='not register' {{ "selected='selected'" if u.reg_status == 'not register' else "" }}>not register</option>
</select>
I'm manually rendering a choice field.
<select name="basisofpricing" id="id_basisofpricing">
{% for value, object in form3.basisofpricing.field.choices %}
<option
value="{{value}}"
{% if form3.basisofpricing.initial.id == value %}
selected
{% endif %}
>
{{object.basisofrate}}
</option>
{% endfor %}
</select>
When I do this, within the browser, the option, when inspected, has quotes and space around it. For example, if Red was one of the options, it would appear like this when being inspected within the browser:
<option value="2">
"
Red
"
</option>
This spacing and these quotes do not appear on the options' text when I just render the choice field with {{form3.basisofpricing}}.
Any thoughts on how I can remove the white spacing and quotes around the option text?
Thanks!
Just try this exactly, without new line or space.
<option value="{{value}}" {% if form3.basisofpricing.initial.id == value %} selected {% endif %}>{{object.basisofrate}}</option>
I'm populating a dropdown box with values from a model as follows (abbreviated):
<select name="dropdown">
{% for thing in storedvalues %}
<option value={{ thing.name }}>{{ thing.name }}</option>
{% endfor %}
</select>
<input type="submit" value="Filter" id="bbutton"/>
and in the view...
def testpage(request):
if request.method == 'POST':
result = request.POST.get('dropdown')
print("hey this is the value: ", result)
selected_result = Song.objects.filter(name=result)
return render(request, 'testpage.html', {'selected_result': selected_result,})
This mostly works correctly. I select a value from a dropdown list and it returns the all of the data relating to the selected value. However when a value has white space in it, any characters following the white space are lost. I'm not sure how to go about fixing this.
If anyone could point me in the right direction it would be much appreciated
Put double quotes around the value otherwise the browser is going to interpret the field as set to empty as such: (yes, I'm using " thing.name" as the value for thing.name - not confusing at all)
<option value="" thing.name=""> thing.name</option>
With the quotes it'll be interpreted as:
<option value=" thing.name"> thing.name</option>
So just add a couple doublequotes and you'll be golden.
<option value="{{thing.name}}">{{ thing.name }}</option>
I'm building a database search engine in Laravel and I am having some problems getting Laravel to select the previously selected item in a dropdown.
Using the template builder options I can make the select like this, and it does what I want it to do:
{{ Form::select('bomserial', $bomserials, Input::get('bomserial'), array('class' => 'pure-input-1', 'tabindex' => '3')) }}
The "Input::Get('bomserial')" makes it re-select the previously selected option in the dropdown after the form has been submitted, but building the input this way means I can't use the "selected disabled" option, so I opted to build the select like this instead:
<select name="bomserial" class="pure-input-1" tabindex="3">
<option selected disabled>BOM Serial</option>
#foreach ($bomserials as $bomserial)
<option value="{{ $bomserial->serial }}">{{ $bomserial->serial }} - {{ $bomserial->job_desc }}</option>
#endforeach
</select>
This produces a better looking menu and allows for the use of a default option, but now I can no longer re-select the previous option after the form has been submitted. How can I get around this?
Since you're not using the Form builder anymore, you have to manually take control of selecting the correct option.
Based on your existing logic, you're looking at something like this: if the bomserial is not in the input, select the placeholder; if the bomserial is in the input, select the bomserial option that matches the input.
<select name="bomserial" class="pure-input-1" tabindex="3">
<option {{ Input::has('bomserial') ? '' : 'selected' }} disabled>BOM Serial</option>
#foreach ($bomserials as $bomserial)
<option value="{{ $bomserial->serial }}" {{ Input::get('bomserial') == $bomserial->serial ? 'selected' : '' }}>{{ $bomserial->serial }} - {{ $bomserial->job_desc }}</option>
#endforeach
</select>