How to send variables from X-DATA to the server. Alpine JS & HTMX & Django? - 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>

Related

How can i get select value in django html?

I have a modal form like this. I want to show a different form in front of the user according to the selected value here, but I can't get the selected value. How can I do that?
<form>
<div class="modal-body">
<div class="mb-3" id="modal-id" style="display: none;">
<label for="id-field" class="form-label">ID</label>
<input type="text" id="id-field" class="form-control" placeholder="ID" readonly />
</div>
<div class="mb-3">
<select class="form-select" aria-label="Disabled select example" name="typeselect">
<option value="1">Type 1</option>
<option value="2">Type 2</option>
<option value="3">Type 3 </option>
<option value="4">Type 4 </option>
</select>
</div>
{% if typeselect.val == "1" %}
<div class="mb-3 typeforms typeone">
{{typeoneform}}
</div>
{% elif typeselect.val == "2" %}
<div class="mb-3 typeforms typetwo">
{{typetwoform}}
</div>
{% elif typeselect.val == "3" %}
<div class="mb-3 typeforms typethree">
{{typethreeform}}
</div>
{% elif typeselect.val == "4" %}
<div class="mb-3 typeforms typefour">
{{typefourform}}
</div>
{% endif %}
</div>
<div class="modal-footer">
<div class="hstack gap-2 justify-content-end">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success" id="add-btn">Add Customer</button>
<button type="button" class="btn btn-success" id="edit-btn">Update</button>
</div>
</div>
</form>
I want to display according to option values ​​from the forms I send to the html page.
Easiest way is to just move the modal div so it is outside any
elements with special positioning. One good place might be just
before the closing body tag .
Alternatively, you can remove the position: CSS properties from the
modal and its ancestors until the problem goes away. This might
change how the page looks and functions, however.

Radio buttons are rendering on top of rest of form

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.

My urls is not updating correctly adding some extra path itself

I want to update the leads while doing so
If you look on the url section it says update_lead/1 which is perfectly right but when i hit update lead i am getting extra update lead in the url section due to which django cant reach its function i am very confuse about it please help
here are my codes from update_lead.html
{% extends 'base.html' %}
{% block exhead %}
{% endblock exhead %}
{% block body %}
<div class="container">
<h2>Update Lead</h2>
<form action="update_lead_handle" method="POST">
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" required name="name" value="{{lead.name}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Subject</label>
<input type="text" class="form-control" id="inputPassword4" name="subject" required value="{{lead.subject}}">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Email</label>
<input type="email" class="form-control" id="inputAddress" name="email" placeholder="abc#email.com" value="{{lead.email}}">
</div>
<div class="form-group col-md-6">
<label for="inputAddress2">Contact Number</label>
<input type="number" class="form-control" id="inputAddress2" name="number"value = "{{lead.mobile_no}}" placeholder="99XX80XXXX">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Source</label>
<select id="inputState" class="form-control" name="source" >
<option selected value="{{lead.source}}">{{lead.source}}</option>
{% for x in source %}
<option value="{{x.name}}">{{x.name}}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Assign To</label>
<select id="inputState" class="form-control" name="assign">
<option selected value="{{lead.assign_to}}">{{lead.assign_to}}</option>
{% for x in agent %}
<option value="{{x.name}}">{{x.name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Status</label>
<select data-target="secondList" class="firstList selectFilter form-control" name="state">
<option selected value="{{lead.state}}">{{lead.state}}</option>
<option data-ref="one">Fresh</option>
<option data-ref="two">Open</option>
<option data-ref="pending">Pending</option>
<option data-ref="close">Close</option>
</select>
<!-- <select id="subject" class="form-control" name="source" >
<option selected value="{{lead.state}}">{{lead.state}}</option>
</select> -->
</div>
<div class="form-group col-md-4">
<label for="inputState">If Pending / Close</label>
<select data-target="thirdList" class="secondList selectFilter form-control" name="pending_close">
<option value="-1">Select</option>
<option data-ref="A" data-belong="close">We Cant Do</option>
<option data-ref="A" data-belong="close">Low Budget</option>
<option data-ref="B" data-belong="close">Client Converted</option>
<option data-ref="C" data-belong="pending">Pending With Customer</option>
<option data-ref="D" data-belong="pending">Pending On Us</option>
<option data-ref="E" data-belong="pending">Pending With Process</option>
<!-- <option data-ref="F" data-belong="three">Second Three</option> -->
</select>
<!-- <select id="topic" class="form-control" name="assign">
<option selected value="{{lead.assign_to}}">{{lead.assign_to}}</option>
</select> -->
</div>
</div>
<!-- <div class="form-group">
<label for="exampleFormControlTextarea1">Initial Followup</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="followup" value=""></textarea>
</div> -->
<button type="submit" class="btn btn-primary">Update Lead </button>
</form>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(".selectFilter").on("change", function () { var e = $(this).data("target"), i = $(this).find(":selected").data("ref"); $("select." + e).val("-1"), null == i ? $("select." + e).find("option").each(function () { console.log("inside undefined"), $(this).removeAttr("disabled hidden") }) : $("select." + e).find("option").each(function () { var e = $(this).data("belong"), t = $(this).val(); i != e && -1 != t ? ($(this).prop("disabled", !0), $(this).prop("hidden", !0)) : ($(this).prop("disabled", !1), $(this).prop("hidden", !1)) }) });
</script>
{% endblock body %}
here are my codes from urls.py
from django.contrib import admin
from django.urls import path
from home import views
urlpatterns = [
path('',views.index,name="index"),
# create agent
path('create_agent_page',views.create_agent_page,name="create_agent_page"),
path('create_agent',views.create_agent,name="create_agent"),
path('signup_page',views.signup_page,name="signup_page"),
path('login_page',views.login_page,name="login_page"),
path('signup_handle',views.signup_handle,name="signup_handle"),
path('login_handle',views.login_handle,name="login_handle"),
path('logout_handle',views.logout_handle,name="logout_handle"),
#Lead handleing
path('create_lead',views.create_lead_page,name="create_lead"),
path('follow_up/<int:id>',views.follow_up,name="follow_up"),
path('update_lead/<int:id>',views.update_lead,name="update_lead"),
path('update_lead_handle',views.update_lead_handle,name="update_lead_handle"),
# path('update_lead',views.update_lead,name="update_lead"),
path('creat_handle_lead',views.creat_handle_lead,name="creat_handle_lead"),
path('lead_list',views.lead_list,name="lead_list"),
]
here are the two views which are requested
def update_lead(request,id):
leads = Lead.objects.get(id = id)
followup = Followup.objects.all
agent = Agent.objects.all
source = Source.objects.all
print(f"the leads are {leads}")
context = {"lead":leads,"followup":followup,"agent":agent,"source":source}
# context = {"lead":leads,"followup":followup}
return render(request,"home/update_lead.html",context)
def update_lead_handle(request):
if request.method == "POST":
organization=get_object_or_404(Organization,user=User.objects.get(username=request.user.username))
name = request.POST.get('name')
subject = request.POST.get('subject')
email = request.POST.get('email')
subject = request.POST.get('subject')
number = request.POST.get('number')
assign= request.POST.get('assign')
source_o= request.POST.get('source')
followup= request.POST.get('followup')
status= request.POST.get('state')
pending_close= request.POST.get('pending_close')
x = Lead.objects.update(name=name,organ = organization,mobile_no = number,source = source_o,subject = subject,message = followup,email= email,assign_to = assign,state = status,closed_or_pending = pending_close)
x.save()
return HttpResponse("The lead has been updated successfully")
else:
return HttpResponse("Babe you are going wrong ")
Kindly put a slash before the update_lead_handle in your form action let it be "/update_lead_handle"

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')