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>
Related
Maybe I'm doing it wrong (tried several ways to achieve my goal) or overlooking something here.
What I'd like to achieve is this:
When I use the Live-search function, I get categories containing the search keyword (like: Paint -> Paint buckets, Paint brushes, Paint colors etc.) which works like a charm. The one thing I need is to style the searched keyword in the presented categories like:
Paint bucket,
Paint brushes,
Color paint
This is the code I have at the moment:
{% if (products.length + categories.length + pages.length) == 0 %}
<div id="Pi56dYpB" class="undefined"> No results for:
<b>{{ query }}</b>...
</div>
{% endif %}
{% if categories.length > 0 %}
<div class="categories">
{% for category in categories %}
{% if loop.index < 7 %}
<a class="p-0" href="{{ category.url }}" style="all:inherit;">
<h3 id="bvwiyjEN" class="undefined">{{ category.name|replace({{ query }}: "<strong>"{{ query }}"<strong>"})|raw }}</h3>
</a>
{% endif %}
{% endfor %}
{% endif %}
Unfortunately this isn't working. I did check if the {{ query }} value is accessible with this simple line of code:
<h3 id="bvwiyjEN" class="undefined">{{ category.name }} - {{ query }}</h3>
No problems found here.
Did I use the wrong syntax in my code maybe? Any help would be appreciated!
replace({{ query }}) is the wrong syntax - replace(query) should work, as you don't want to echo the variable query here
As Nico Haase pointed out already, {{ ... }} is used to output variables, this translate to something like <?php echo $foo; ?> and can't/shouldn't be used inside statements
Furthermore, the filter replace expects an array of keys to replace with the values.
You would need to change your code to the following:
{% for category in categories %}
{{ category|replace({ (query): '<strong>'~query~'</strong>',})|raw }}
{% endfor %}
demo
Some notes:
The filter replace uses the PHP function strtr in the background. This mean the output will be case-sensitive. If you don't want this, I'd advice you to follow through here
Wrapping the key with parantheses is mandatory. This will force twig to evaluate/interpolate the value of the key - demo
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
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 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>