HTMX hx-trigger two functions - html

I have a selected dropdown list that triggers a function in my Django application.
So I need to execute another function too. is it possible?
this is the HTML code I tried to use hx-trigger with load after 0.5s but is executed once before the user selects the item!
then I moved the function inside the page that it run after the user selects the item. it works but the returned request is duplicated! and I need it to replace the old list?
<div class="col-md-3">
<select class="custom-select mb-4" name="fields" hx-get="{% url 'select_field' %}" hx-trigger="change" hx-target="#Wells" hx-include="[name='Toggle']">
<option selected>Select a field</option>
{% for Field in TFTFields %}
<option ue="{{Field.Perimeter}}">{{Field.Perimeter}}</option>
{% endfor %}
</select>
</div>
<div class="row">
<div class="col-md-12" >
<div id="Wells">
{% include 'Home/Data_visa/part_Wells.html' %}
</div>
</div>
</div>
<div class="col-md-12" >
<div id="part_MyWellsList">
{% include 'Home/Data_visa/part_MyWellsList.html' %}
</div>
</div>
And this is my part_Wells.html page
<div class="col-md-3" >
<select class="custom-select mb-4" name="Wells" hx-get="{% url 'addplotly' %}" hx-trigger="change" hx-target="#plotly_production" hx-include="[name='Toggle']">
<option selected>Select Well</option>
{% for well in TFTWells %}
<option value="{{well.WellID}}">{{well.WellID}}</option>
{% endfor %}
</select>
</div>
<div class="col-md-12"
hx-target="#part_MyWellsList"
hx-get="{% url 'LoadWells' %}"
hx-trigger="load"
hx-include="[name='fields']"
hx-swap="afterend">
</div>
and this part_MyWellsList page:
{% if FieldWells %}
<div class="row">
<div class="col-md-3" >
<select class="custom-select mb-4" name="Wells1" hx-get="{% url 'addplotly' %}" hx-trigger="change" hx-target="#plotly_production" hx-include="[name='Toggle']">
<option selected>Select Well</option>
{% for well in FieldWells %}
<option value="{{well.WellID}}">{{well.WellID}}</option>
{% endfor %}
</select>
</div>
</div>
{% endif %}
with this structure everything in the backend works well just I need the last dropdown list to be replaced with the new one?

I solved it by making these changes.
the main page:
<div class="col-md-3">
<select class="custom-select mb-4" name="fields" hx-get="{% url 'select_field' %}" hx-trigger="change" hx-target="#Wells" hx-include="[name='Toggle']">
<option selected>Select a field</option>
{% for Field in TFTFields %}
<option ue="{{Field.Perimeter}}">{{Field.Perimeter}}</option>
{% endfor %}
</select>
</div>
<div class="row">
<div class="col-md-12" >
<div id="Wells">
{% include 'Home/Data_visa/part_Wells.html' %}
</div>
</div>
</div>
<div class="col-md-12" >
<div id="part_MyWellsList">
</div>
</div>
and also in the second pages as:
<div class="col-md-12"
hx-target="#part_MyWellsList"
hx-get="{% url 'LoadWells' %}"
hx-trigger="load"
hx-include="[name='fields']"
hx-swap="innerHTML">
{% include 'Home/Data_visa/part_MyWellsList.html' %}
</div>

Related

Django displays source code instead of web page

I am new to django. Any help will be appreciated. It displays source code instead of web page. To be specific the base page (base.html). What I want is to use the data from patient details and doctors detail as they are from different groups. I think that the problem is when i pass my dictionary.
Views.py
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
f = {'doctor_details': doctor_details}
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
d = {'patient_details': patient_details}
return render(request, 'booking.html', f, d)
Html
{% extends 'base.html' %}
{% block content %}
{% load static %}
<div class="loginContainer">
<div class="img">
<img src="{% static 'image/booking.svg' %}">
</div>
<div class="login-content">
{% for d in patient_details %}
<form method="POST" id="signupForm">
{% for f in doctors_details %}
{% csrf_token %}
<h2 class="title">Booking form</h2>
<div class="input-div one">
<div class="i">
<ion-icon name="person-circle"></ion-icon>
</div>
<div class="div">
<h5>Patient ID: PID - {{d.id}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="person"></ion-icon>
</div>
<div class="div">
<h5>Name: {{d.FirstName}} {{d.LastName}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="business"></ion-icon>
</div>
<div class="div">
<h5>Department</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="medkit"></ion-icon>
</div>
<div class="div">
<h5>{{f.name}}</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="bandage"></ion-icon>
</div>
<div class="div">
<h5>Symptoms</h5>
<input type="textarea" class="input" name="address" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="document-text"></ion-icon>
</div>
<div class="div">
<h5>Comments (Optional)</h5>
<input type="textarea" class="input" name="address">
</div>
</div>
<button type="submit" class="loginBtn">Submit</button>
Return to Personal Profile
Cancel Booking
{% endfor %}
</form>
{% endfor %}
</div>
</div>
{% ifequal error 'no' %}
<script type="text/javascript">
alert("You have successfully registered.")
window.location = ('loginPage')
</script>
{% endifequal %}
{% ifequal error 'yes' %}
<script type="text/javascript">
alert("Something went wrong.")
</script>
{% endifequal %}
{% endblock %}
Urls.py
from django.urls import path
from . import views
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index),
path('contact', views.contact),
path('loginPage', views.loginPage, name='login'),
path('doctorlogin', views.doctorlogin, name='doclogin'),
path('help', views.help),
path('signup', views.signup, name='signup'),
path('doctors', views.doctors, name='doctors'),
path('booking', views.booking, name='booking'),
path('userProfile', views.userProfile, name='userProfile'),
path('doctorProfile', views.doctorProfile, name='doctorProfile'),
path('logout', views.logout, name='logout')
]
urlpatterns = urlpatterns + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You should pass context as one dictionary. You can use update method to add new items to your dictionary:
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
f = {'doctor_details': doctor_details}
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
f.update({'patient_details': patient_details}) # Using update
return render(request, 'booking.html', f)
Thank you everyone for contributing. I found my mistake. The problem was in the way i use the views.py. The above answer is correct but i prefer to use it this way.
Views.py
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
d = {'patient_details': patient_details,
'doctor_details': doctor_details} #edited part
return render(request, 'booking.html', d)
And for the html part.
Html
{% extends 'base.html' %}
{% block content %}
{% load static %}
<div class="loginContainer">
<div class="img">
<img src="{% static 'image/booking.svg' %}">
</div>
<div class="login-content">
<form method="POST" id="signupForm">
{% csrf_token %}
<h2 class="title">Booking form</h2>
{% for f in patient_details%} #here is the change
<div class="input-div one">
<div class="i">
<ion-icon name="person-circle"></ion-icon>
</div>
<div class="div">
<h5>Patient ID: PID - {{f.id}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="person"></ion-icon>
</div>
<div class="div">
<h5>Name: {{f.FirstName}} {{f.LastName}}</h5>
</div>
</div>
{% endfor %}
<div class="input-div one">
<div class="i">
<ion-icon name="business"></ion-icon>
</div>
<div class="div">
<h5>Department</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="medkit"></ion-icon>
</div>
<div class="div">
<div class="custom-select">
<select name="doctors" required>
{% for f in doctor_details %} #here is the change
<option value="{{f.name}}">{{f.name}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="bandage"></ion-icon>
</div>
<div class="div">
<h5>Symptoms</h5>
<input type="textarea" class="input" name="address" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="document-text"></ion-icon>
</div>
<div class="div">
<h5>Comments (Optional)</h5>
<input type="textarea" class="input" name="address">
</div>
</div>
<button type="submit" class="loginBtn">Submit</button>
Return to Personal Profile
Cancel Booking
</form>
</div>
</div>
{% ifequal error 'no' %}
<script type="text/javascript">
alert("You have successfully registered.")
window.location = ('loginPage')
</script>
{% endifequal %}
{% ifequal error 'yes' %}
<script type="text/javascript">
alert("Something went wrong.")
</script>
{% endifequal %}
{% endblock %}

Django Formset one form in each row

How to place each form of a formset on a horizontal line, one below the other without using a table?
My html is putting everything on one line.
Any help much appreciated.
Html
[<div class="form-group row">
<div class="row">
{{ formset.management_form }}
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
{% endfor %}
</div>
</div>
image from screen
This reorganization solved the problem.
Thanks for the help!
<div class="form-group row">
<div class="col-sm-12">
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="row">
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
</div>
{% endfor %}
</div>
</div>
Please try using </br> (break line) command:
<h1> HEllo</h1> </br> <h2>How are you</h2>

Symfony - pass GET parameters to another route?

I have a table with filters, and i want to download the rows as csv, filtered.
My function to get filters from form:
public function getFilters($request)
{
$filters = [
'stato' => $request->get('stato'),
'cliente' => $request->get('cliente'),
'registrar' => $request->get('registrar'),
'creazione_inizio' => $request->get('creazione_inizio'),
'creazione_fine' => $request->get('creazione_fine'),
'scadenza_inizio' => $request->get('scadenza_inizio'),
'scadenza_fine' => $request->get('scadenza_fine'),
'scadenza_pagamento_inizio' => $request->get('scadenza_pagamento_inizio'),
'scadenza_pagamento_fine' => $request->get('scadenza_pagamento_fine'),
];
return $filters;
}
On the page where the table is i have this code and it works fine:
/**
* #Route("/dashboard", name="dashboard")
*/
public function dashboard(Request $request, PaginatorInterface $paginator): Response
{
$filters = $this->getFilters($request);
}
But on the route to export csv i have this and $filters is always null:
/**
* #Route("/report/report.csv", name="domains_data_csv")
*/
public function exportDomainsDataCsvAction(Request $request)
{
$filters = $this->getFilters($request);
}
I have the filters form and the button to download csv on the dashboard page.
Any tips to make it work?
EDIT
My dashboard page is made like this:
<div class="mb-2">
<span class="h2">Dashboard</span>
</div>
<div class="mb-3">
{{ include('Admin/includes/_search.html.twig') }}
</div>
{{ include('Admin/includes/_filter.html.twig') }}
{% if domains %}
<div class="col-auto">
Scarica CSV
</div>
{{ include('Admin/includes/_domains_table.html.twig',{ 'domains':domains }) }}
{% if is_granted('ROLE_ADMIN') %}
<a class="btn btn-primary" aria-current="page" href="{{path('new_domain')}}" role="button">Nuovo dominio</a>
{% endif %}
{% else %}
{% endif %}
Filters form:
<div class="card">
<div class="row align-items-center">
<a class="text-decoration-none text-reset" id="filter-header" href="#collapseExample" data-bs-toggle="collapse" aria-expanded="false">
<div class="card-header d-flex align-items-center">
<h5 class="pe-3">Filtri</h5>
<div class="border-1 bg-success border border-success w-100" style="opacity:unset;"></div>
<i class="bi bi-chevron-right ps-3 fs-3 text-success arrow"></i>
</div>
</a>
</div>
<div class="collapse" id="collapseExample">
<div class="card-body">
<form id="form-filter-status" method="GET">
{% if is_granted('ROLE_ADMIN') %}
<div class="row mb-3 justify-content-center">
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Stato:</div>
<div class="col-sm">
<select name="stato" class="form-select" onchange='this.form.submit();'>
<option value="" {% if filters.stato is empty %} selected {% endif %}>Seleziona...</option>
<option value="1" {% if filters.stato == 1 %} selected {% endif %}>Attivo</option>
<option value="2" {% if filters.stato == 2 %} selected {% endif %}>Richiesta Dismissione</option>
<option value="3" {% if filters.stato == 3 %} selected {% endif %}>Dismesso</option>
<option value="4" {% if filters.stato == 4 %} selected {% endif %}>Richiesto Trasferimento</option>
<option value="5" {% if filters.stato == 5 %} selected {% endif %}>Trasferito</option>
</select>
</div>
</div>
</div>
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Cliente:</div>
<div class="col-sm">
<select name="cliente" class="form-select" onchange='this.form.submit();'>
<option value="" {% if filters.cliente is empty %} selected {% endif %}>Seleziona...</option>
<option value="1" {% if filters.cliente == 1 %} selected {% endif %}>KINETIKON s.r.l.</option>
<option value="2" {% if filters.cliente == 2 %} selected {% endif %}>CINQUEBIT s.r.l.</option>
<option value="3" {% if filters.cliente == 3 %} selected {% endif %}>JAKALA S.p.A.</option>
<option value="4" {% if filters.cliente == 4 %} selected {% endif %}>IN.SI. s.r.l.</option>
<option value="5" {% if filters.cliente == 5 %} selected {% endif %}>PAGNOSSIN s.r.l.</option>
<option value="6" {% if filters.cliente == 6 %} selected {% endif %}>ELEPHASE s.r.l.</option>
</select>
</div>
</div>
</div>
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Register:</div>
<div class="col-sm">
<select name="registrar" class="form-select" onchange='this.form.submit();'>
<option value="" {% if filters.registrar is empty %} selected {% endif %}>Seleziona...</option>
<option value="1" {% if filters.registrar == 1 %} selected {% endif %}>Register</option>
<option value="2" {% if filters.registrar == 2 %} selected {% endif %}>OpenProvider</option>
<option value="3" {% if filters.registrar == 3 %} selected {% endif %}>TowerTech</option>
<option value="4" {% if filters.registrar == 4 %} selected {% endif %}>Aruba</option>
<option value="5" {% if filters.registrar == 5 %} selected {% endif %}>Aruba Business</option>
</select>
</div>
</div>
</div>
</div>
{% endif %}
<div class="row justify-content-center">
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Creazione</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="creazione_inizio">Da:</span>
<input type="date" name="creazione_inizio" class="form-control" value="{{filters.creazione_inizio}}" placeholder="Creazione inizio" aria-label="Creazione inizio" aria-describedby="creazione_inizio">
</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="creazione_fine">A:</span>
<input type="date" name="creazione_fine" class="form-control" value="{{filters.creazione_fine}}" placeholder="Creazione fine" aria-label="Creazione fine" aria-describedby="creazione_fine">
</div>
<div class="d-grid gap-2">
<button class="btn btn-outline-success" type="submit">Filtra data creazione</button>
</div>
</div>
</div>
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Scadenza</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="scadenza_inizio">Da:</span>
<input type="date" name="scadenza_inizio" class="form-control" value="{{filters.scadenza_inizio}}" placeholder="Scadenza inizio" aria-label="Scadenza inizio" aria-describedby="scadenza_inizio">
</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="scadenza_fine">A:</span>
<input type="date" name="scadenza_fine" class="form-control" value="{{filters.scadenza_fine}}" placeholder="Scadenza fine" aria-label="Scadenza fine" aria-describedby="scadenza_fine">
</div>
<div class="d-grid gap-2">
<button class="btn btn-outline-success" type="submit">Filtra data scadenza</button>
</div>
</div>
</div>
<div class="card col-sm-3 mx-3">
<div class="card-body">
<div class="card-title">Scadenza pagamento</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="scadenza_pagamento_inizio">Da:</span>
<input type="date" name="scadenza_pagamento_inizio" class="form-control" value="{{filters.scadenza_pagamento_inizio}}" placeholder="Scadenza pagamento inizio" aria-label="Scadenza pagamento inizio" aria-describedby="scadenza_pagamento_inizio">
</div>
<div class="input-group mb-3">
<span class="input-group-text col-2" id="scadenza_pagamento_fine">Da:</span>
<input type="date" name="scadenza_pagamento_fine" class="form-control" value="{{filters.scadenza_pagamento_fine}}" placeholder="Scadenza pagamento fine" aria-label="Scadenza pagamento fine" aria-describedby="scadenza_pagamento_fine">
</div>
<div class="d-grid gap-2">
<button class="btn btn-outline-success" type="submit">Filtra data scadenza pagamento</button>
</div>
</div>
</div>
</div>
{% if query is defined %}
<input type="hidden" name="query" value="{{query}}">
{% endif %}
</form>
</div>
</div>
To pass the current GET parameters (from URL, not form) You should be able to modify the href as follows:
Scarica CSV
To use the current form values you need to use JavaScript to set the form's action attribute value to that returned by {{ path('domains_data_csv') }} and submit the form.

bootstrap panel class captures the content of above div

I got 2 divs, one with panel bootstrap class, another without, and I need to place this panel div below the regular one. Now it looks like this:
but when I place my panel div below regular one I get the following:
so the upper content is capured by panel div. The code is as follows:
<div class="container">
<div class="form_container" style="float:left;">
<div class="form-group col-md-12" style="margin-top: 20px;">
<div class="col-md-6">
<div class="input-group pull-left">
<input id="message" type="text" class="form-control" placeholder="Введите фразу и нажмите Enter" autofocus="" autocomplete="off">
<span class="input-group-btn">
<button class="btn btn-default btn-add" type="button" id="chat_send">Отправить</button>
</span>
</div>
</div>
<div class="form-group col-md-6">
<label for="server" class="col-xs-2 control-label" style="padding-left: 14%;padding-right: 10%;margin-top: 5px;">Сервер</label>
<div class="col-md-8">
<select class="form-control" id="server" name="server_field">
{% for server_addr in server_address %}
{% for k, v in server_addr %}
<option value="{{ v }}">{{ k }}</option>
{% endfor %}
{% endfor %}
</select>
</div>
</div>
</div>
</div>
<div class="panel panel-default" style="margin-top: 1%;" >
<div class="panel-body" style="padding-top: 0;padding-bottom: 0;">
<div class="form-group col-md-8" style="margin-top:12px;">
<label for="phrase_group" class="col-xs-3 control-label" style="margin-top: 5px;">Выберите группу</label>
<div class="col-md-8">
<select class="form-control" id="phrase_group" name="phrase_group" style="width:40%; float:left;">
<option value="{{ group.id }}">{{ group.phraseset }}</option>
{% for group in group_phrases %}
<option value="{{ group.id }}">{{ group.phraseset }}</option>
{% endfor %}
</select>
<button id="test_phraseset" class="btn btn-primary" style="margin-left: 10%;" data-id="2">Тест</button>
<button id="clear_phraseset" class="btn btn-primary" style="margin-left: 7%;" data-id="2">Очистить</button>
</div>
</div>
</div>
</div>
</div>
Any ideas how to fix that? thank you.

Bootstrap misaligned label under textarea

I have a bootstrap form which consists of basic input fields and a few textarea's. Im using Django for my form validation and am having a persistent problem aligning validation labels under the textarea's. This form is only using bootstrap css modules so there is no custom css interfering with it as far as i can tell.
Example: The date and time field align perfectly as the notes validation label does not.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="well well-lg">
<form class="form-horizontal" action="" autocomplete="off" method="post">
{% csrf_token %}
{% for field in UserForm %}
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
</div>
{% for error in field.errors %}
<div class="col-md-6">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
</div>
{% endfor %}
</form>
</div>
</div>
</div>
The number of columns inside the class="row" does not add up to 12. Try rewriting the form-group part like this:
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
{% for error in field.errors %}
<p>
<strong>{{ error|escape }}</strong>
</p>
{% endfor %}
</div>
</div>