I'm new in Django and now I'm working on my first project.
I created a form in bootstrap in django. Data from inputs are used to update previous set of data in model nom and to processing in view home.
How can I use previous data from nom database as default values in input. Is there any easy way?
Thanks
bootstrap form
<div class="container my-container">
<form action="{% url 'home' %}" method="Post">
{% csrf_token %}
<div class= "row my-row">
<div class="col-4 my-col">
Specyfikacja z dni:
</div>
<div class="col-4 my-col">
<input type="date" placeholder="0" name="datebeg" size="1" />
</div>
<div class="col-4 my-col">
<input type="date" placeholder="0" name="dateend" size="1" />
</div>
</div>
<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>
views
from django.shortcuts import render
from . import slownie
from .models import nom
from django.conf import settings
import copy
def home (request):
ls=''
ls1=''
ls3=''
liczba= request.POST.get("li200")
liczba1=request.POST.get("li100")
liczba3=request.POST.get("li50")
datebeg=request.POST.get("datebeg")
dateend = request.POST.get("dateend")
nom200 = liczba
nom100 = liczba1
nom50 = liczba3
if datebeg=='':
datebeg='1000-10-10'
if dateend=='':
dateend='1000-10-10'
nom.objects.update(
nom200=nom200,
nom100=nom100,
nom50=nom50,
datebeg=datebeg,
dateend=dateend
)
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})
models
from django.db import models
from datetime import date
from django.utils.timezone import now
class nom (models.Model):
nom200 = models.CharField(max_length=200,null=True, blank=True)
nom100 = models.CharField(max_length=200,null=True, blank=True)
nom50 = models.CharField(max_length=200,null=True, blank=True)
datebeg=models.DateField(null=True,blank=True, auto_now_add=True)
dateend=models.DateField(null=True,blank=True, auto_now_add=True)
def __str__(self):
return self.nom200
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…