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>
Related
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
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>
This is the html file
<label for="caller">Caller</label>
<select name="caller">
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
</select>
I want to dynamically preset the value of the caller. How can I do that?
E.G sometimes I want it to be 20, sometimes it can be 22. The value I want to send is stored in a variable and available as {{caller}}
You are passing caller from view function to template and you are trying to show default selected value then use selected="selected"
<option {% if caller == "20" %}selected="selected"{% endif %}>20</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>
I have problem with models for example i have
fallBack = models.CharField(max_length=100, choices=FALLBACK, default = 99,blank = True,null = True)
FALLBACK = (
(u'99','-'),
(u'standard', u'Standard TPF deactivation/exlusion applies'),
(u'fallback', u'Fallback script provided'),
(u'na',u'N/A'),
(u'other',u'Other'),
)
if i will use in template
{% for obj in form.forms %}
{{obj.fallBack}}
{% endfor %}
it will return sthing like this
<select name="form-0-fallBack" id="id_form-0-fallBack">
<option value="">---------</option>
<option value="99" selected="selected">-</option>
<option value="standard">Standard TPF deactivation/exlusion applies</option>
<option value="fallback">Fallback script provided</option>
<option value="na">N/A</option>
<option value="other">Other</option>
</select>
But i wanna make something like this:
<select **onchange="javascript:make()"** name="form-0-fallBack" id="id_form-0-fallBack">
<option value="">---------</option>
<option value="99" selected="selected">-</option>
<option value="standard">Standard TPF deactivation/exlusion applies</option>
<option value="fallback">Fallback script provided</option>
<option value="na">N/A</option>
<option value="other">Other</option>
</select>
How to do that ??
I can write in template this secound option instead of {{obj.fallBack}} but then my formset.is_valid() dont work , and when i will do something like this:
1)I will choose from fallBack second option(Standard TPF deactivation/exlusion applies)
2) submit,
3) I have some error in other place in template
4) Then my obj.fallBack returning to default option (99 , -)
Thanks for help
Check out the django documentation on Widgets
widget=forms.Select(attrs={'onchange': 'javascript:make()'})
This looks like a ModelForm, check out how to specify widgets for ModelForms