Dropdown are not working in Django , HTML - html

I am trying to display data from database throw drop-down list and Use filter query to filter data and display
if i tried to select value of html and select but slot is not create. Debugging form and i see error. If i select anything but drop-down do't selected here is error
Here is my code: slot/views.py
def slot_create(request):
form = SlotForm(request.POST)
print(form)
station = Station.objects.filter(user_id=request.user)
print(station)
if form.is_valid():
slot = form.save(commit=False)
slot.user = request.user
slot.save()
messages.success(request, 'Your Slot is add successfully')
return redirect('view_slot')
return render(request, 'add_slot.html', {
'form': form,
'station': station
})
here is my add_slot.html
<form method="post" novalidate>
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-12 mb-0">
{{ form.slot_name|as_crispy_field }}
</div>
<div class="form-group col-md-12 mb-0">
{{ form.per_unit_price|as_crispy_field }}
</div>
<div class="form-group col-md-6 mb-0">
{{ form.current_status|as_crispy_field }}
</div>
{% comment %} <div class="form-group col-md-6 mb-0">
{{ form.station|as_crispy_field }}
</div> {% endcomment %}
<div class="form-group col-md-6 mb-0">
<label for="station" class="block font-semibold text-sm mb-1"> Choose Station </label>
<select name="station_name" id = "station">
{% for station in station %}
<option value="{{station}}"> {{station}} </option>
{% endfor %}
</select>
</div>
<button type="submit" class="w-full rounded-full bg-red-gradient p-3 text-white font-bold hover:ring">
Add Slot
</button>
</div>
</form>

Related

Django ask for fill a form not required

I've a strange bug with my forms.
In a page I must hande 2 separate forms,
the 1st one have no problem at all and I can add the record in DB ,
the 2nd one raise me an error asking to fill a required data of 1st form.
The POST dictionary looks ok
Here the log:
<QueryDict: {'csrfmiddlewaretoken': ['lvt5Ph2SA1xxFK4LMotdHOWk2JuZzYDo0OKWc77rKICYKYmemy3gl0dBphnRNcFb'], 'pk_atto': ['1.1'], 'pk_persona': ['1'], 'capacita': ['11'], 'Aggiungi_persona': ['persona'], 'tipo': ['fondo']}>
<ul class="errorlist"><li>pk_particella<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
the views:
if request.method == 'POST':
if("Aggiungi_particella" in request.POST):
save_atto = AttiPartenzeParticelleForm(request.POST)
else:
save_atto = AttiPartenzePersoneForm(request.POST)
print(request.POST)
print(save_atto.errors)
if save_atto.is_valid():
save_atto.save()
return redirect('/aggiungi_atto_partenza' + '/' + str(save_atto['pk_atto'].value()))
the forms:
class AttiPartenzeParticelleForm(ModelForm):
pk_atto = forms.ModelChoiceField(queryset=Atti.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
pk_particella = forms.ModelChoiceField(queryset=Particelle.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
capacita = forms.CharField(max_length=30,
widget=forms.NumberInput
(attrs={'class': 'form-control'}))
tipo = forms.CharField(max_length=30, initial="fondo", widget=forms.TextInput (attrs={'class': 'form-control'}))
class Meta:
model = Acquisizioni_Cessioni_particella
fields = '__all__'
class AttiPartenzePersoneForm(ModelForm):
pk_atto = forms.ModelChoiceField(queryset=Atti.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
pk_persona = forms.ModelChoiceField(queryset=Persone.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
capacita = forms.CharField(max_length=30,
widget=forms.NumberInput
(attrs={'class': 'form-control'}))
tipo = forms.CharField(max_length=30, initial="fondo", widget=forms.TextInput (attrs={'class': 'form-control'}))
class Meta:
model = Acquisizioni_Cessioni_particella
fields = '__all__'
and the HTML
<div id="particella" class="content-section d-flex justify-content-center mt-5">
<form action="" method="POST" id="particella_f">
{% csrf_token %}
<fieldset class="form-group">
<div style="visibility:hidden">
{{ form.pk_atto|as_crispy_field }}
</div>
<div class="row">
<div class="col-8">
{{ form.pk_particella|as_crispy_field }}
</div>
<div class="col-2">
{{ form.capacita|as_crispy_field }}
</div>
<div class="col-4 d-flex justify-content-center">
<button form="particella_f" class="btn btn-outline-primary btn-lg mt-5" type="submit" name="Aggiungi_particella" value="particella">
AGGIUNGI PARTICELLA
</button>
</div>
<div style="visibility:hidden">
{{ form.tipo|as_crispy_field }}
</div>
</div>
</fieldset>
</form>
</div>
<div id="persona" class="content-section d-flex justify-content-center mt-5">
<form action="" method="POST" id="persona_f">
{% csrf_token %}
<fieldset class="form-group">
<div style="visibility:hidden">
{{ persona.pk_atto|as_crispy_field }}
</div>
<div class="row">
<div class="col-8">
{{ persona.pk_persona|as_crispy_field }}
</div>
<div class="col-2">
{{ persona.capacita|as_crispy_field }}
</div>
<div class="col-4 d-flex justify-content-center">
<button class="btn btn-outline-primary btn-lg mt-5" form="persona_f" type="submit" name="Aggiungi_persona" value="persona">
AGGIUNGI PERSONA
</button>
</div>
<div style="visibility:hidden">
{{ form.tipo|as_crispy_field }}
</div>
</div>
</fieldset>
</form>
</div>
soneone have any idea?
thx

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.

How to Create search function in Flask using Sqlite3

This is my form for search function
<form class="form-inline ml-auto" action="{{ url_for('search') }}" method="POST">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="search" id="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
Now I wan to display it if its match so this is my html
{% for articles in data %}
<div class="card m-3">
<input type="hidden" value="{{ data[0] }}" id="id" name="id">
<h5 class="card-header">{{ data[0] }}</h5>
<div class="card-body">
<h5 class="card-title">{{ data[2] }} - {{ data[3] }}</h5>
<p class="card-text">{{ data[4] }}</p>
</div>
<div class="row m-3 ml-auto">
<div class="btn-group mr-2">
Update Article
</div>
<div class="btn-group">
Delete Article
</div>
</div>
</div>
{% endfor %}
And also this is my query filter
#app.route('/search',methods = ['POST', 'GET'])
def search_route():
if request.method == 'POST':
q= request.args.get('search')
with sqlite3.connect("blogdb.db") as con:
cur = con.cursor()
cur.execute('SELECT * FROM articles WHERE title = ?', (q))
art = cur.fetchall()
cur.close()
print(art[0])
con.commit()
return render_template('search.html', data = art[0])
So far what I got is that it doesnt return anything I also tried to print(art[0]) so that I can see what did i get but i aint getting anything.

How to edit a BoleanField in Django

please i need your help been stuck like this for days,here is my problem, i have a profile page of a user that contain a BooleanField and a Button if the user would like to subscribe or not, i am wanting to design it this way, in which their will only be a one click button that will show Subscribe and once clicked you you get redirected to the payment page then you choose your plan and make payment,then in the user info it will show a dead btn of Subscribed and underneath would be the code generated for a specific task,just help me share even if its a link to a tutorial to follow as a starting point,here is my code and the picture
model.py
class Patient(models.Model):
STATE_CHOICES=(
(True, u'Yes'),
(False, u'No'),
)
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, related_name="patient")
subscribe = models.BooleanField(default=True, choices=STATE_CHOICES)
html
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 h4 text-center center">Your Email</legend>
<legend class=" mb-4 h3 center text-center">{{ user.email }}</legend>
</fieldset>
<div class="form-group text-center center">
<button class="btn btn-outline-info" type="submit">{{ user.patient.subscribe }} </button>
</div>
</form>
Im not sure what exactly you want. But it looks like you want to use if statement in your template. Something like this.
{% if not user.patient.subscribed %}
<button class="btn btn-outline-info" type="submit">{{ user.patient.subscribe }}</button>
{% else %}
Subscribed!
{% endif %}
https://docs.djangoproject.com/en/3.0/ref/templates/language/
how do i generate a random numbers or text as plan id
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 h4 text-center center">Your Email</legend>
<legend class=" mb-4 h3 center text-center">{{ user.email }}</legend>
</fieldset>
<div class="form-group text-center center">
{% if not user.patient.subscribe %}
<button class="btn btn-outline-info" type="submit">Subscribe</button>
{% else %}
{% if user.patient.subscribe %}
<button class="btn btn-danger" type="submit">Subscribed</button>
<p>Thanks For Subscribing here is your Plan ID </p>
{% endif %}
{% endif %}
</div>
</form>

submit button in django refreshes the page without saving

I am trying to save data in ORM via a form but when I click on the submit button all it does is refresh the page with the same data.
HTML
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy form %}
{% load widget_tweaks %}
{% block content %}
<h2 class="mb-3">Add a Warehouse</h2>
<div class="row">
<form method="post" class="col-md-12 proct-form" novalidate>
{% csrf_token %}
<div class="form-group col-md-4 mb-0">
{{ form.owner|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_name|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_email|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_contact|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_city|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_pincode|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_state|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_pan|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_gst|as_crispy_field }}
</div>
<div class="form-group col-md-12 mb-0">
<button type="submit" class="btn btn-success">Save</button>
Nevermind
</div>
</form>
</div>
{% endblock %}
Views.py
#method_decorator([login_required, employee_required], name='dispatch')
class WarehouseFormView(CreateView):
model = Warehouse
fields = "__all__"
template_name = 'packsapp/employee/warehouseForm.html'
def form_valid (self, form):
product = form.save(commit=False)
product.save()
messages.success(self.request, 'The Warehouse was created with success!')
return redirect('employee:warehouse_table')
Is there a problem with my views.py or do I need to change my templates?
All the other forms in the app are created the same way and they work fine