in twig file after spliting the value i'm getting output like this in a browser
<select class="form-control" id="eventCustomerID" name="eventCustomer">
<option value="64" name="64">64</option>
<option value="demou22_6hhh" name="demou22_6hhh">demou22_6hhh</option>
</select>
but i need an output to be like this
<select class="form-control" id="eventCustomerID" name="eventCustomer">
<option value="64" name="64">demou22_6hhh</option>
</select>
this is my code in a twig file
<select class="form-control" id="eventCustomerID" name="eventCustomer">
{% set tags = 64|demou22_6hhh|split('|',1) %}
{% for x in tags %}
<option value="{{ x }}" name="{{ x }}">{{ x }}</option>
{% endfor %}
</select>
I am stuck with this can someone help me out with this issue.
To achieve your required output, you shouldn't use a for loop, but access the elements directly.
<select class="form-control" id="eventCustomerID" name="eventCustomer">
{% set tags = "64|demou22_6hhh"|split('|') %}
<option value="{{ tags[0] }}">{{ tags[1] }}</option>
</select>
demo
Note about the second argument you are passing to the filter split
Not sure why you are passing 1 as the second argument. By setting this parameter to 1, you are limiting the output to one element in the array. More information here.
Related
I 'm trying to find a workable solution for my problem. I have found two similar questions answered before, but still I can't solve it. If we have a class like this:
from django.db import models
class Consumer(models.Model):
SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
size = models.CharField(max_length=2, choices=SIZES)
And I did in my view and template like this (Learned from one tutorial)
***view with combined queries***
def staff_filter(request):
qs = Consumer.objects.all()
size= request.GET.get('size')
# I have some other queries in between ....
if is_valid_queryparam(size) and size!='Choose...':
qs = qs.filter(size=consumer.get_size.display())
return qs
def filter(request):
qs=staff_filter(request)
context={
'queryset':qs,
'consumer':consumer.objects.all()
}
return render(request, 'filter.html',context)
**template***
<div class="form-group col-md-4">
<label for="size">size</label>
<select id="size" class="form-control" name="size">
<option selected>Choose...</option>
{% for size in consumer.get_size.display %}
<option value="{{ size }}">{{size}}</option>
{% endfor %}
</select>
</div>
How should I correct it? Thanks!
Display selection field in Django template
You mispelled consumer in context in view
'consumer':consumer.objects.all() #You mispelled consumer here
Try this:
'consumer':Consumer.objects.all() # Consumer, first letter should be capital
I think you misplelled in above code so that's why you are not getting values on template
You do not need any get_size.display() method in a template. If consumer variable is an object of the Consumer class then you just have to do like this:
<select>
{% for val, text in consumer.SIZES %}
<!--
Here SIZES is a tuple variable of your Consumer class,
while consumer is an instance of the Consumer class then you can just
call SIZES tuple from any of its instances.
-->
<option value="{{ val }}">{{ text }}</option>
{% endfor %}
</select>
If consumer is a variable from the context you are passing in your
view, then it's a queryset. So in this case you should loop over your queryset first, then call SIZES variable from every object inside the loop:
{% for object in consumer %}
<select>
{% for val, text in object.SIZES %}
<option value="{{ val }}">{{ text }}</option>
{% endfor %}
</select>
{% endfor %}
I am trying to make a form in Angular with a select box whose values are taken from an array. I also need the form to show inputs based on the value of that select. This is the code:
<select #freq formControlName="freq" class="m-2">
<option disabled>Select</option>
<option value="">Select Option</option>
<option *ngFor="let x of freq_list" [ngValue]="x">{{x}}</option>
</select>
<div *ngIf="freq.value!=''">
{{freq.value}}
<div *ngFor="let x of parameter_config[freq.value]">
<input type="number" formControlName="input1">
</div>
</div>
where freq_list is a simple string[]=['option1',option2',option3'] and parameter_config is a map {[id:string]: string[];}where the id is the a value of freq_list.
And this is the form creation:
form=this.fb.group({
freq:'',
});
Everything works except that the value returned by the selected option is not simply 'option1' but it is preceded by the order in the list (e.g. '1: option1', '2: option2', ...)
Is there something I am missing? How is this the default behavior?
replace ngValue with value
<option *ngFor="let x of freq_list" [ngValue]="x">{{x}}</option>
should be
<option *ngFor="let x of freq_list" [value]="x">{{x}}</option>
I have a list a = [[1000,3000], [4000-5000]], i would like to show it has
1000-3000
4000-5000 in template. But i'm getting only the first object.
What i tried is,
products.html
{% for i in price_group %}
<label>
<input class="uk-radio" type="radio" onchange="$('#form').submit();" name="price" value="{{ i.0.0 }};{{ i.0.1 }};price;first" checked>
{{ i.0.0 }} - {{ i.0.1 }}
</label>
{{ price_group.pop.i }}
{% endfor %}
I used pop functionality to remove current from the list and take the next one, but it's not working as expected.
it is showing only the first element in the list. Please correct me if I am wrong. What I want is it has a price range like.
1000-3000
4000-5000..like wise. Please help me.
You are accessing only first element of the list, you need to update your code to access all the elements like this:
{% for i,j in price_group %}
<label> <input class="uk-radio" type="radio" onchange="$('#form').submit();" name="price" value="{{ i }};{{ j }};price;first" checked>
{{ i }} - {{ j }}
</label>
{% endfor %}
I am passing two objects 'editsub_obj' and 'cat' both key-value pairs to the template.
The main aim is to keep option tag selected when name from both object list matches but the code is not working.
<select class="form-control" id="" name="cat_name" required>
{% if not editsub_obj %}
{% for data in cat %}
<option value="{{data.id}}">{{data.category_name}}</option>
{% endfor %}
{% else %}
{% for data in cat %}
{% if editsub_obj.category_name == data.category_name %}
<option value="{{data.id}}" selected>{{data.category_name}}</option>
{% else %}
<option value="{{data.id}}">{{data.category_name}}</option>
{% endif %}
{% endfor %}
{% endif %}
</select>
Expected :
selected should be selected with the category_name matches in both objects list.
Actual :
none of the options are showing as selected.
Your code can reduce to ::
<select class="form-control" id="" name="cat_name" required>
{% for data in cat %}
<option value="{{data.id}}"
{% if editsub_obj and editsub_obj.category_name in data.category_name %}
selected
{%endif%}>
{{data.category_name}}
</option>
{% endfor %}
</select>
I have two select list that are equal how do i access the second one?
I have put a value on the first one but when putting a value to the second it goes to the first.
Anyone can help me.
<div id="selectNumber">
<select id="numbers" name="numbers" tabindex="5" onchange="changeNumbers(this);">
<option value="" selected="selected"></option>
<option value="all">25</option>
<option value="123">26</option>
<option value="124">27</option>
</select>
<a onclick="addRemoveSelectedNumber(this);">
<img src="../../static/images/delete.png">
</a>
</div>
<div id="selectNumber">
<select id="numbers" name="numbers" tabindex="5" onchange="changeNumbers(this);">
<option value="" selected="selected"></option>
<option value="all">25</option>
<option value="123">26</option>
<option value="124">27</option>
</select>
<a onclick="addRemoveSelectedNumber(this);">
<img src="../../static/images/delete.png">
</a>
Tanks
This will select 25 from the second select list:
browser.select_list(:id => "numbers", :index => 1).select "25"
More information: Multiple Attributes chapter at https://github.com/zeljkofilipin/watirbook/blob/master/link.md
By the way, you should report a bug that there are multiple instances of HTML elements with the same id on the page. Per the HTML standard, ID values should be unique