Radio buttons are rendering on top of rest of form - django

I'm using vanilla bootstrap with Python on Django. I've configured a form with radio buttons, however the buttons render on top of the fields as shown in the screenshot below.
I've notice that Django puts the radio buttons into a list and I thought that could be the cause, so I tried using CSS to disable the tag but the radio buttons still float on top just without the list bullets.
Forms.py
class MerchantGroupForm(forms.Form):
DO_WHAT_CHOICES=[('merge','Merge'),
('update','Update')]
#do_what = forms.ChoiceField(choices=DO_WHAT_CHOICES, widget=forms.RadioSelect(attrs={'class': "custom-radio-list"}))
do_what = forms.ChoiceField(choices=DO_WHAT_CHOICES, widget=forms.RadioSelect)
merge_merchantgroup = forms.ModelChoiceField(required=False, queryset=MerchantGroup.objects.all().order_by('name'), empty_label="Merge with")
name = forms.CharField(required=False)
default_ledger = GroupedModelChoiceField(required=False, queryset=Ledger.objects.all().order_by('coa_sub_group__name','name'), choices_groupby = 'coa_sub_group')
disable_AI = forms.BooleanField(required=False, label='Disable AI')
<form action="/monzo/merchantgroups/update/799/" method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="ZWtsz3JDsnUtu1mj6NO3SDlBuyJyEpDgbZUDC6elfTPK2DCwWevD2BpirSZJOhiM">
<div class="form-group">
<label for="id_do_what_0">Do what:</label>
<ul id="id_do_what" class="custom-radio-list form-control">
<li><label for="id_do_what_0"><input type="radio" name="do_what" value="merge" class="custom-radio-list form-control" required id="id_do_what_0">
Merge</label>
</li>
<li><label for="id_do_what_1"><input type="radio" name="do_what" value="update" class="custom-radio-list form-control" required id="id_do_what_1">
Update</label>
</li>
</ul>
</div>
<div class="form-group">
<label for="id_merge_merchantgroup">Merge merchantgroup:</label>
<select name="merge_merchantgroup" class="form-control" id="id_merge_merchantgroup">
<option value="" selected>Merge with</option>
<option value="203">ATM</option>
<option value="799">Amazon</option>
<option value="200">Post Office</option>
<option value="201">Virgin Media</option>
<option value="202">www.modelsport.co.uk</option>
</select>
</div>
<div class="form-group">
<label for="id_name">Name:</label>
<input type="text" name="name" value="Amazon" class="form-control" id="id_name">
</div>
<div class="form-group">
<label for="id_default_ledger">Default ledger:</label>
<select name="default_ledger" class="form-control" id="id_default_ledger">
<option value="">---------</option>
<optgroup label="Accounts">
<option value="20">Jacks Account</option>
<option value="32">Jacks Monzo</option>
<option value="21">Janes Account</option>
<option value="126">Janes Monzo</option>
<option value="22">Joint Account</option>
</optgroup>
<optgroup label="Bills">
<option value="7">Council tax</option>
<option value="6">Electricity & Gas</option>
</optgroup>
<optgroup label="Cash">
<option value="23">Jacks Float</option>
<option value="24">Janes Float</option>
</optgroup>
<optgroup label="Food & Home">
<option value="8">Basic food</option>
<option value="9">Coffee & Snacks</option>
<option value="11">Home Supplies</option>
<option value="10">Take away</option>
</optgroup>
</optgroup>
<optgroup label="Property">
<option value="19">Property</option>
</optgroup>
<optgroup label="Purchases">
<option value="31">Computer & Electrical</option>
<option value="30">Hobbies</option>
<option value="29" selected>Toys</option>
</optgroup>
<optgroup label="Reserves">
<option value="27">Profit & Loss Accounts</option>
</optgroup>
<optgroup label="Taxation">
<option value="5">Income tax</option>
</optgroup>
<optgroup label="Taxation">
<option value="25">Tax Liability</option>
</optgroup>
<optgroup label="Travel">
<option value="17">Public Transport</option>
<option value="18">Taxis</option>
</optgroup>
</select>
</div>
<div class="form-group">
<label for="id_disable_AI">Disable AI:</label>
<input type="checkbox" name="disable_AI" class="form-control" id="id_disable_AI" checked>
</div>
<input type="submit" class="btn btn-primary">
</form>

If the screenshot you showed have template the previous .html code then Django does nothing because you don't use Django form to render your form. It's just a plain HTML/Bootstrap form.
This is how to use Django Form.
Also if you want to Group radios on the same horizontal row with Bootstrap then you can add .form-check-inline to any .form-check as Bootstrap inline radio button page says.
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="merge"
value="merge">
<label class="form-check-label" for="merge">Merge</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="update"
value="update">
<label class="form-check-label" for="update">update</label>
</div>
NB : I think that your first .form-group is not well structured.
You can visit Bootstrap form to do it fine.

Removing the form-control class did the trick. According to Bootstrap docs this class is only for certain fields, guess radio is not one of them.

Related

How to send variables from X-DATA to the server. Alpine JS & HTMX & Django?

I have got the next snippet. There are a few select blocks. What I would like to do is to collect both variables and send to the Django server. But the request.GET is empty. What is wrong?
<div class="flex" x-data="{ foo: '', bar: ''}">
<div class="flex justify-between">
<div class="mt-1">
<select x-model="foo"
name="foo"
id="foo"
class="w-64 h-14 text-sm border-gray-300 focus:bg-transparent">
<option value="">Type of FOO</option>
<option value="FOO_1">FOO_1</option>
<option value="FOO_2">FOO_2</option>
<option value="FOO_1">FOO_3</option>
</select>
</div>
<div class="mt-1">
<select name="bar"
x-model="bar"
id="bar"
class="w-64 h-14 text-sm border-gray-300 focus:bg-transparent">
<option value="">BAR Types</option>
<option value="BAR_1">BAR_1</option>
<option value="BAR_2">BAR_2</option>
<option value="BAR_3">BAR_3</option>
</select>
</div>
<input type="text" name="foo" x-model="foo" hidden />
<input type="text" name="bar" x-model="bar" hidden />
<button
hx-get="{% url 'server:parse-values' %}"
hx-target="#element"
hx-swap="innerHTML"
hx-include="[name='foo', name='bar']">
<span
class="cursor-pointer px-3 py-3 border-2">Parse details</span>
</button>
</div>
</div>
But when I click on the button the Django backend does not receive foo and bar parameters.
Any thoughts?
To include multiple inputs you need to separate them with a comma like this:
hx-include="input[name='foo'], input[name='bar']"
That would be the equivalent of using querySelectorAll to grab your inputs:
document.querySelectorAll("input[name='foo'], input[name='bar']")
I added the additional input so it excludes the select lists, but you can probable remove the hidden inputs from your example and just include the lists.
I should also add that if you move the htmx attributes from the button to the x-data div and change that into a form. Then change the button to type submit. All the form data will be automatically sent to the server.
<form class="flex" x-data="{ foo: '', bar: ''}"
hx-get="{% url 'server:parse-values' %}"
hx-target="#element"
hx-swap="innerHTML"
>
<div class="flex justify-between">
<div class="mt-1">
<select x-model="foo"
name="foo"
id="foo"
class="w-64 h-14 text-sm border-gray-300 focus:bg-transparent">
<option value="">Type of FOO</option>
<option value="FOO_1">FOO_1</option>
<option value="FOO_2">FOO_2</option>
<option value="FOO_1">FOO_3</option>
</select>
</div>
<div class="mt-1">
<select name="bar"
x-model="bar"
id="bar"
class="w-64 h-14 text-sm border-gray-300 focus:bg-transparent">
<option value="">BAR Types</option>
<option value="BAR_1">BAR_1</option>
<option value="BAR_2">BAR_2</option>
<option value="BAR_3">BAR_3</option>
</select>
</div>
<button type="submit">
<span class="cursor-pointer px-3 py-3 border-2">Parse details</span>
</button>
</form>

How to populate select option dropdown with database info in django

I have a django project with a select dropdown. I have typed in the names of players that users can select, but i want to populate the dropdown with the same names but dynamically from the database. How can i go about this?
This is my html so far, not sure how to write the views.py for this, or if i should do a for loop or and if.
<form method="POST">
{% csrf_token %}
<select id="playerselect" name="pitcher" class="player-dropdown">
<option value="{{ pitching.id}}">{{ pitching.player_name }}</option>
<option value="2">Yadiel Lugo</option>
<option value="3">Cody Reeds</option>
<option value="4" >Xavier Colon</option>
<option value="5" >Andy Smith</option>
<option value="6" >Carson Rex</option>
<option value="7" >Jalen Jackson</option>
<option value="8" >Matthew Cobbs</option>
<option value="9" >Matt Sampson</option>
<option value="10" >John Harrison</option>
<option value="11" >Robert Santiago</option>
<option value="12" >Efrain Zuniga</option>
<input type="submit" value="Search"/>
</select>
In view.py get all the options that you want to see in your HTML
options = yourmodels.objects.filter(foo=bar)
Then pass it in your context dictionary
context = {'options': options, ...}
Then In Your HTML
<select id="playerselect" name="pitcher" class="player-dropdown">
{% for option in options %}
<option value="{{ option.id}}">{{ option.player_name }}</option>
{% endfor %}
<input type="submit" value="Search"/>
</select>

Select in template does not update in edit

I have the following code below, which would be the editing of a form of a request I made here for my work, as I had to change some of the views my update has to be manual, and the select field is not getting the result that I'm bringing it from the db, all fields are working except the select.
as it is now ||| as should be
class EditPedido(View):
def get(self, request, venda):
data = {}
venda = fixa.objects.get(id=venda)
data['filial'] = venda.regional
return render(request, 'fixa/fixa_update.html', data)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">---------</option>
{% for filial in filiais %}
<option value="{{ filial.id }}">{{ filial.nome }}</option>
{% endfor %}
</select>
EDIT
class EditPedido(View):
def get(self, request, venda):
empresa_logada = request.user.funcionario.empresa
data = {}
data['filiais'] = empresa.objects.filter(nome=empresa_logada)
venda = fixa.objects.get(id=venda)
data['filial'] = venda.regional
return render(request, 'fixa/fixa_update.html', data)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">---------</option>
{% for filial in filiais %}
<option value="{{ filial.id }}">{{ filial.nome }}</option>
{% endfor %}
</select>
<form method="POST" action="{% url 'create_fixa' %}">
{% csrf_token %}
<h2>number do Pedido: {{ sell.number }}</h2>
<p><font color="RED">{{MSG}}</font></p>
<hr>
<br>
<div class="form-row">
<div class="form-group col-md-3 mb-0">
<label for="number">Nº da Simulação</label>
<input type="text" name="number" class="form-control" value="{{number}}" required="">
</div>
<div class="form-group col-md-9 mb-0">
<label for="razao">Razão Social</label>
<input type="text" name="razao" class="form-control" value="{{razao}}" required="">
</div>
</div>
<br>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label for="razao">CNPJ</label>
<input type="text" name="cnpj" class="form-control" data-mask="00.000.000/0000-00" value="{{cnpj}}" required="">
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">TIPO</label>
<select name="tp_cli" class="select form-control" required="" id="tp_cli">
<option value="" selected="">---------</option>
<option value="FRESH">FRESH</option>
<option value="BASE">BASE</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label for="razao">Filial</label>
<select name="filial" class="select form-control" required="" id="id_regional">
{% for filial in filiais %}
<option value="{{filial.id }}">{{ filial.nome }} {% if filial.id == filial %}selected{% endif %}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">Indicação</label>
<select name="indicacao" class="select form-control" id="indicacao">
<option value="" selected="">---------</option>
{% for indicacao in indicaoes %}
<option value="{{ indicacao.id }}">{{ indicacao.nm_primeiro_nome }} {{indicacao.nm_segundo_nome}}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">Status</label>
<select name="status" class="select form-control" id="status">
<option value="" selected="">---------</option>
{% for statu in status %}
<option value="{{ statu.id }}">{{ statu.nome_st }}</option>
{% endfor %}
</select>
</div>
</div>
<br>
<input type="hidden" value="{{sell.id}}" name="sell_id">
<button type="submit" class="btn btn-success">Salvar</button>
</form>
class EditOrder(View):
def get(self, request, sell, *args, **kwargs):
company_entered = request.user.employee.company
parent_company = request.user.employee.company.company_pai
data = {}
data['status'] = tb_status.objects.all()
sell = fixa.objects.get(id=sell)
if parent_company:
data['filiais'] = company.objects.filter(nome=company_entered)
data['indicaoes'] = employee.objects.filter(cargo__nome='Vendedor', company__nome=company_entered)
else:
data['filiais'] = company.objects.filter(nome=company_entered) | company.objects.filter(
company_pai=company_entered)
data['indicaoes'] = employee.objects.filter(cargo__nome='Vendedor')
data['number'] = sell.number
data['razao'] = sell.nm_razao
data['cnpj'] = sell.nr_cnpj
data['tp_cli'] = sell.tipo_cli
data['filia'] = sell.regional_id
data['indicacao'] = sell.indicacao
data['sell'] = sell
data['family'] = tb_tp_prod.objects.all()
data['type'] = tb_tipo_servico.objects.all()
data['qtds'] = tb_qtd.objects.all()
data['itens'] = sell.itemdopedido_set.all()
return render(request, 'fixa/fixa_update.html', data)
So this is what you had in your last comment (I don't know where the space before the words in the third and fourth options are coming from, but perhaps you should remove them)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">--------</option>
<option value="1">TESTE</option>
<option value="2"> SOROCABA</option>
<option value="5"> MARILIA</option>
</select>
You have the 1st option selected, which is --------. I think you want this
<select name="filial" class="select form-control" required="" id="filial">
<option value="1">TESTE</option>
<option value="2"> SOROCABA</option>
<option value="5" selected=""> MARILIA</option>
</select>
You will have to indicate to the template which option to select, you could pass this as a variable something like
data['selection'] = 5
Then in the template
<option value="{{ filial.id }}" {% if filial.id == selection %}selected=""{% endif %}>{{ filial.nome }}</option>
I'm not certain about the syntax of the {% if filial.id == selected %} statement. I don't use it much but the idea is to just put the tag in the option that requires it.

How to use Ember action helper in element append to the DOM

First, I wanna let you know that I'm just starting at Ember, so, if is there a better way to accomplish the same result, feel free to point at that.
I have a component called "select-room", it should work as the fields "Quartos", "Adultos" and "Crianças" at this site.
In this site when the field "Quartos" is changed, will be added or removed more selects fields on the form, the same happen when the value of "Crianças" change. I was able to accomplish this behavor using DOM manipulation, but when appending a select field like this html+= ''; the action helper don't work.
Link to the full code is at this repo in Github: https://github.com/darwinboaventura/embercvc
What is the best to way to solve this problem?
You'd use data set on the component to render DOM elements using handlebars syntax.
{{#if items}}
<select onchange={{action ..}}>
{{#each items as |item|}}
<option value={{item.value}}>{{item.label}}</option>
{{/each}}
</select>
{{/if}}
See this example: https://guides.emberjs.com/v2.14.0/templates/displaying-a-list-of-items/
Overall, you should go through the tutorial, so you have a better grasp of how things work in Ember. Start here: https://guides.emberjs.com/v2.14.0/tutorial/ember-cli/
I solved the problem, here my solution:
app/components/select-rooms.js:
import Ember from 'ember';
export default Ember.Component.extend({
name: null,
quantityRooms: 1,
rooms: '',
init() {
this._super(...arguments);
this.set('rooms', [1]);
},
actions: {
changeQuantityRooms(value) {
this.set('quantityRooms', value);
this.set('rooms', []);
for (var i = 0; i < this.get('quantityRooms'); i++) {
this.get('rooms').pushObject(i);
};
},
changeQuantityChildren(value) {
this.set("quantity" + value.name, value.value);
this.set(value.name, []);
for (var i = 0; i < this.get("quantity" + value.name); i++) {
this.get(value.name).pushObject(i);
}
}
}
});
app/templates/components/select-rooms.hbs:
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Quartos</label>
<select name={{name}} onchange={{action "changeQuantityRooms" value="target.value"}} class="form-control">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<div class="col-sm-6">
{{#if rooms}}
{{#each rooms as |room index|}}
<div class="row">
<div class="col-sm-6 form-group">
<label>
Adultos <small>+18</small>
</label>
<select name={{concat name index "-adults"}} class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
<div class="col-sm-6 form-group">
<label>
Crianças <small>até 17 anos</small>
</label>
<select name={{concat name index "-children"}} onchange={{action "changeQuantityChildren" value="target"}} class="form-control">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
<div class="col-xs-12">
{{#if (get this (concat "quantity" name index "-children"))}}
<div class="row">
<div class="col-xs-12">
<label>Idade das crianças: </label>
</div>
</div>
<div class="row">
{{#each (get this (concat name index "-children")) as | child i |}}
<div class="col-sm-4 form-group">
<select name={{concat name index "-children-age-" i}} class="form-control">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
</select>
</div>
{{/each}}
</div>
{{/if}}
</div>
</div>
{{/each}}
{{/if}}
</div>
</div>

Django - custom controls HTML in crispy forms

I try to use Crispy forms for ModelForm in Django. However, I need custom HTML code for all fields.
Here is sample field code generated by crispy forms:
<div class="span12 field-box">
<div id="div_id_user" class="control-group">
<label for="id_user" class="control-label requiredField">
User select
<span class="asteriskField">*</span>
</label>
<div class="controls">
<select id="id_user" class="select" name="user">
<option value="" selected="selected">---------</option>
<option value="1">User 1</option>
<option value="2">User 2</option>
</select>
</div>
</div>
</div>
but I would need code generated like:
<div class="span12 field-box">
<label>User:</label>
<div class="ui-select span5">
<select>
<option value="1">User 1</option>
<option value="2">User 2</option>
</select>
</div>
</div>
I think it would be enough if I will be able to just add custom class to div.controls - But I have no idea how to achieve that.
Here is crispy form code:
self.helper.layout = Layout(
Div(
Field('user'),
css_class="span12 field-box",
),
)
How about setting a class in field layout object:
Field('user', css_class='span5')