Symfony - pass GET parameters to another route? - csv

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.

Related

HTMX hx-trigger two functions

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>

Media files uploaded by the user are not getting displayed

Using django for rendering user uploaded data from a "/blogs/createblog" form web page to "/blogs" webpage, but the image file uploaded by the user is not getting displayed in his/her blogpost ("/blogs") page. My settings.py, base level/urls.py, app/models.py, app/views.py and the html templates are something like this:
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
baseproject/urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.urls import re_path
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home.urls')),
path('blogs/',include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
app/models.py
from django.db import models
from django.utils.timezone import now
from django.contrib.auth.models import User
# Create your models here.
class BlogPost(models.Model):
blog_id=models.AutoField(primary_key=True)
author=models.CharField(max_length=200)
title=models.CharField(max_length=300)
pub_date=models.DateField()
category=models.CharField(max_length=200,default="Promotional Blogs")
heading1=models.CharField(max_length=300,blank=True)
content1=models.TextField(blank=True)
heading2=models.CharField(max_length=300)
content2=models.TextField()
about=models.TextField()
likes=models.ManyToManyField(User, related_name="blogpost_like")
image=models.ImageField(upload_to="blog/images")
def number_of_likes(self):
return self.likes.count()
def __str__(self):
return self.title + " by " + self.author
app/views.py
def createblog(request):
if request.method=="POST":
author=request.POST.get("author")
title=request.POST.get("title")
today=date.today()
category=request.POST.get("category")
heading=request.POST.get("heading")
body=request.POST.get("body")
about=request.POST.get("about")
pic=request.POST.get("pic")
new_post=BlogPost(author=author,title=title,pub_date=today,category=category,heading2=heading,content2=body,about=about,image=pic)
new_post.save()
messages.success(request,"Your blog is published successfully.")
return redirect("/blogs")
return render(request,"blog/editor.html")
app/editor.html
{% extends 'blog/basic.html' %}
{% block title %}Editor - BlogLikho{% endblock %}
{% block body %}
<div class="container my-4">
<h1 class="mb-4" style="text-align:center;">Blog Editor</h1>
<form method="POST" action="/blogs/createblog">{% csrf_token %}
<div class="row">
<div class="mb-3 col-md-6">
<label for="author" class="form-label" style="font-weight: bold;">Author</label>
<input type="text" class="form-control" id="author" name="author" placeholder="" required>
</div>
<div class="mb-3 col-md-6">
<label for="title" class="form-label" style="font-weight: bold;">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Blog title" required>
</div>
</div>
<div class="mb-3">
<label for="category" class="form-label" style="font-weight: bold;">Category</label>
<select class="form-select" aria-label="Default select example">
<option selected>Open this select menu</option>
<option value="Coding Blogs">Coding Blogs</option>
<option value="Sports Blogs">Sports Blogs</option>
<option value="Traveling Blogs">Traveling Blogs</option>
<option value="Educational Blogs">Educational Blogs</option>
<option value="Business Blogs">Business Blogs</option>
<option value="Marketing Blogs">Marketing Blogs</option>
<option value="Technology Blogs">Technology Blogs</option>
<option value="Sales Blogs">Sales Blogs</option>
</select>
<input type="hidden" name="category" value="Coding Blogs">
</div>
<div class="mb-3">
<label for="heading" class="form-label" style="font-weight: bold;">Heading</label>
<input type="text" class="form-control" id="heading" name="heading" placeholder="" required>
</div>
<div class="mb-3">
<label for="body" class="form-label" style="font-weight: bold;">Body</label>
<textarea class="form-control" id="body" name="body" rows="4" required></textarea>
</div>
<div class="mb-3">
<label for="about" class="form-label" style="font-weight: bold;">About you</label>
<textarea class="form-control" id="about" rows="3" name="about" placeholder="Write a bit about yourself" required></textarea>
</div>
<div class="mb-3">
<label for="pic" class="form-label" style="font-weight: bold;">Blog display image</label>
<input class="form-control" type="file" id="pic" name="pic">
</div>
<button type="submit" class="publish btn btn-success">Publish</button>
</form>
</div>
{% endblock %}
app/yourblogs.html
{% extends "blog/basic.html" %}
{% block activeblogs %}active{% endblock %}
{% block title %}Blogs - BlogLikho{% endblock %}
{% load static %}
{% block body %}
<div class="container my-4">
<div class="row mb-2">
{% for blog_item in myblogs %}
<div class="col-md-6">
<div class="row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
<div class="col p-4 d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-success">{{blog_item.author}}</strong>
<h4 class="mb-0">{{blog_item.title | truncatechars:25 }}</h4>
<div class="mb-1 text-muted">{{blog_item.pub_date}}</div>
<p class="card-text mb-auto" style="margin-bottom: revert!important;">{{blog_item.content1 | truncatewords:10}}</p>
<button type="button" class="btn btn-primary" style="width: fit-content;">Continue reading</button>
</div>
<div class="col-auto d-none d-lg-block">
<img src="/media/{{blog_item.image}}" class="bd-placeholder-img" width="250" height="250" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
What might be the problem?? The error which i am getting by clicking on the image url inside the specific BlogPost ImageField is..
404:Page not found
Please suggest me the solution.

define an element as default value in a list created by a for loop in a django template

here is my html
<div class="modal-body">
<form action="#" id="periodeOptionForm">
<div class=" card-body">
<div class="form-group form-elements">
<div class="form-label">Options</div>
<div class="custom-controls-stacked">
{% for periode in periodes %}
<label class="custom-control custom-radio" id'{{periode.labelle}}'>
<input type="radio" class="custom-control-input" name="example-radios" value="{{periode.valeur}}">
<span class="custom-control-label">{{periode.labelle}} </span>
</label>
{% endfor %}
</div>
</div>
</div>
</form>
</div>
My fbv
def periodes(request):
...
periodes = [{"labelle":"10 minutes","valeur":10},{"labelle":"30 minutes","valeur":30},{"labelle":"1 heure","valeur":60}]
....
return render(request,"my_template.html",{"periodes":periodes}
When I display the html page no elements are selected. I want the first element of my periodes list to be selected by default. How can I do it?
You can use forloop.first around checked:
<input type="radio" class="custom-control-input" name="example-radios" value="{{periode.valeur}}" {% if forloop.first %}checked{% endif %}>

Checkboxes are not the same height as input boxes - Bootstrap

I want the checkboxes on the left side to be the same height as the text input boxes on the right.
HTML:
{% block content %}
<h1>{{ls.name}}</h1>
<form method="post" action="#">
{% csrf_token %}
{% for item in ls.item_set.all %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
{% if item.complete == True %}
<input type="checkbox", value="clicked", name="c{{item.id}}" checked>
{% else %}
<input type="checkbox", value="clicked", name="c{{item.id}}">
{% endif %}
</div>
</div>
<input type="text", value="{{item.text}}" class="form-control">
</div>
{% endfor %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<button type="submit", name = "newItem", value="newItem" class="btn btn-primary">Add Item</button>
</div>
<input type="text", name="new">
</div>
<button type="submit", name = "save", value="save" class="btn btn-primary">Save</button>
</form>
{% endblock %}
give you check box class then in you linked css give checkbox height and width acc to your need
<div class="input-group-text">
{% if item.complete == True %}
<input class="check" type="checkbox", value="clicked", name="c{{item.id}}" checked>
{% else %}
<input class="check" type="checkbox", value="clicked", name="c{{item.id}}">
{% endif %}
then in your css
.check {
height:20px;
width:20px;
}

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.