I'm new in Django and now I'm working on my first project.
My home.html file includes form (bootstrap class) witch contains entries to fill with data. These data are integer type. method='GET
At the bottom of the form i have two submit buttons. My intention is that first of them is going to grab data from entries, calculate them and show results on site of browser using home function from views. Second button is going to grab the same data, calculate them and use them for conversion to pdf by ReportLab by some_view function in views. My questions are.
- How to keep imputed values in entries (now these fields are cleared after clicking submit button)
- How can I use these two buttons separately as I mentioned above. Now I can make only one button active and I have to choose If I want use one function or another.
views.py
import reportlab
import io
from django.http import FileResponse
from reportlab.pdfgen import canvas
#==================================
from django.shortcuts import render
from . import slownie
def home (request):
ls=''
ls1=''
ls3=''
liczba= request.GET.get("li200")
liczba1=request.GET.get("li100")
liczba3=request.GET.get("li50")
if liczba and liczba.isdigit():
liczba=int(liczba)*200
ls=slownie.slownie(int(liczba))
if liczba1 and liczba1.isdigit():
liczba1=int(liczba1)*100
ls1=slownie.slownie(int(liczba1))
if liczba3 and liczba3.isdigit():
liczba3=int(liczba3)*50
ls3=slownie.slownie(int(liczba3))
suma=0
if liczba1 and liczba and liczba3:
suma=int(liczba)+int(liczba1)+int(liczba3)
return render(request, 'home.html',{'liczba':liczba,'liczba1':liczba1,'liczba3':liczba3,
'suma':suma,
'ls':ls,
'ls1':ls1,
'ls3':ls3})
def some_view(request):
ls = ''
ls1 = ''
ls3 = ''
liczba = request.GET.get("li200")
liczba1 = request.GET.get("li100")
liczba3 = request.GET.get("li50")
if liczba and liczba.isdigit():
liczba = int(liczba) * 200
ls = slownie.slownie(int(liczba))
if liczba1 and liczba1.isdigit():
liczba1 = int(liczba1) * 100
ls1 = slownie.slownie(int(liczba1))
if liczba3 and liczba3.isdigit():
liczba3 = int(liczba3) * 50
ls3 = slownie.slownie(int(liczba3))
suma = 0
# Create a file-like buffer to receive PDF data.
buffer = io.BytesIO()
# Create the PDF object, using the buffer as its "file."
p = canvas.Canvas(buffer)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
p.drawString(100, 100, ls3)
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
# FileResponse sets the Content-Disposition header so that browsers
# present the option to save the file.
buffer.seek(0)
return FileResponse(buffer , as_attachment=True, filename='hello.pdf')
home.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<title>Title</title>
<style>
body { margin: 40px }
.my-container {
border: 1px solid green}
.my-row {
border: 2px solid blue}
.my-col {
border: 2px solid red}
btn-primary { margin-left: 50px}
.align-right {
text-align: center;
border: 0;
}
</style>
</head>
<body>
<div class="container my-container">
<form action="{% url 'home' %}" method="get">
<div class= "row my-row">
<div class="col-3 my-col">
<input type="text" placeholder="0" name="li200" size="1" />
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if liczba %}
{{ liczba }}
{% endif %}
</span></h3>
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if ls %}
{{ ls }}
{% endif %}
</span></h3>
</div>
</div>
<div class= "row my-row">
<div class="col-3 my-col">
<input type="text" placeholder="0" name="li100" size="1" />
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if liczba1 %}
{{ liczba1 }}
{% endif %}
</span></h3>
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if ls1 %}
{{ ls1 }}
{% endif %}
</span></h3>
</div>
</div>
<div class= "row my-row">
<div class="col-3 my-col">
<input type="text" placeholder="0" name="li50" size="1" />
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if liczba3 %}
{{ liczba3 }}
{% endif %}
</span></h3>
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary" name="superowo"> {% if ls3 %}
{{ ls3 }}
{% endif %}
</span></h3>
</div>
</div>
<div class= "row my-row">
<div class="col-3 my-col">
</div>
<div class="col my-col">
<h3><span class="badge badge-secondary"> {% if suma %}
{{ suma }}
{% endif %}
</span></h3>
</div>
</div>
<input type="submit" class="btn" value="Click" href="{% url 'home'%}" name="print_btn">
<a class="btn btn-primary" type="submit" href="{% url 'some_view'%}" >Do PDF</a>
<div class="align-right">
</div>
</form>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>