Why doesn't the Django formset appear? - html

I have one form and one formset on the same page. Prior to my deletion of information from django admin, I could see both the form and the formset. After I deleted some 'patient' information and updated my css file, the formset stopped showing.
views.py
def patient_new(request):
patientForm = PatientForm()
formset = LocationFormSet()
if request.method == "POST":
patientForm = PatientForm(request.POST)
formset = LocationFormSet(request.POST)
if patientForm.is_valid() and formset.is_valid():
patient = patientForm.save(commit=True)
for form in formset:
location = form.save(commit=False)
location.patient = patient
location.save()
return index(request)
else:
print('ERROR FORM INVALID')
return render(request, 'covidapp/patient_new.html',{'patientForm':patientForm, 'formset':formset})
html file
<form class="form-horizontal" method="POST">
{% csrf_token %}
<div class="jumbotron">
<div class="row spacer">
<div class="col-2">
<div class="input-group">
{{ patientForm.as_p }}
</div>
</div>
</div>
{{ formset.management_form }}
{% for form in formset %}
<div class="row form-row spacer">
<div class="col-6">
<div class="input-group">
{{ form.as_p }}
<div class="input-group-append">
<button type="button" class="btn btn-success add-form-row">+</button>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="row spacer">
<div class="col-4 offset-2"></div>
<button type="submit" class="btn btn-block btn-primary">Create</button>
</div>
</div>
</form>
html output
I am not sure whether the hidden input is supposed to be my formset and why it is hidden.
model.py
class Patient(models.Model):
name = models.CharField(max_length=200)
idn = models.IntegerField(unique=True)
date_of_birth = models.DateField()
date_of_confirm = models.DateField()
case_number = models.IntegerField()
def get_absolute_url(self):
return reverse("patient_detail", kwargs={'pk':self.pk})
def __str__(self):
return self.name
class Location(models.Model):
patient = models.ForeignKey(Patient, related_name='locations', on_delete=models.CASCADE)
location_name = models.CharField(max_length=50, null=True)
address = models.CharField(max_length=300, null=True)
grid_x = models.IntegerField(null=True)
grid_y = models.IntegerField(null=True)
date_from = models.DateField(null=True)
date_to = models.DateField(null=True)
details = models.CharField(max_length=300, null=True)
category = models.CharField(max_length=300, null=True)
def __str__(self):
return self.location_name
form.py
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = '__all__'
LocationFormSet = modelformset_factory(
Location,
extra=0,
exclude = ('patient',)
)

Related

Django ChoiceField with custom forms

I am relatively new to Django and have been following up some tutorials on how to create registration forms with Django.
This is the html form code
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
<div class="form-row">
<div class="col form-group">
<label>First name</label>
{{ form.first_name }}
</div> <!-- form-group end.// -->
<div class="col form-group">
<label>Last name</label>
{{ form.last_name }}
</div> <!-- form-group end.// -->
</div> <!-- form-row end.// -->
<div class="form-row">
<div class="col form-group">
<label>Email</label>
{{ form.email }}
</div> <!-- form-group end.// -->
<div class="form-group col-md-6">
<label>Directorate</label>
<select name="directorate" class="form-control" required>
<option value="" disabled selected>Select</option>
{% for i in form.directorate %}
<option value=""></option>
{% endfor %}
</select>
</div> <!-- form-group end.// -->
</div> <!-- form-row end.// -->
<div class="form-row">
<div class="form-group col-md-6">
<label>Create password</label>
{{ form.password }}
</div> <!-- form-group end.// -->
<div class="form-group col-md-6">
<label>Repeat password</label>
{{ form.confirm_password }}
</div> <!-- form-group end.// -->
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block"> Register </button>
</div> <!-- form-group// -->
</form>
models.py
class Account(AbstractBaseUser):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=50, unique=True)
email = models.EmailField(max_length=100, unique=True)
directorate = models.CharField(max_length=100)
# required
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superadmin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name']
objects = MyAccountManager()
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .forms import RegistrationForm
from .models import Account
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
username = email.split('#')[0]
password = form.cleaned_data['password']
directorate = form.cleaned_data['directorate']
user = Account.objects.create_user(first_name=first_name, last_name=last_name, email=email, directorate=directorate, username=username, password=password)
user.save()
else:
form = RegistrationForm()
context = {
'form': form,
}
return render(request, 'accounts/register.html', context)
forms.py
from django import forms
from .models import Account
DIRECTORATE_CHOICES = [
("E", "Education"),
("H", "Health"),
("T", "Transport"),
]
class DirectorateChoices(forms.Form):
directorate = forms.ChoiceField(choices=DIRECTORATE_CHOICES,widget=forms.Select(attrs={
'class':'form-control'}), required=True)
class RegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Enter your password',
}))
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Confirm your password',
}))
class Meta:
model = Account
fields = ['first_name', 'last_name', 'email', 'directorate', 'password']
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs['placeholder'] = 'First name'
self.fields['last_name'].widget.attrs['placeholder'] = 'Last name'
self.fields['email'].widget.attrs['placeholder'] = 'Email address'
self.fields['directorate'].widget.attrs['placeholder'] = 'Select your directorate'
self.fields['password'].widget.attrs['placeholder'] = 'Enter password'
self.fields['confirm_password'].widget.attrs['placeholder'] = 'Confirm password'
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
I however want to introduce a ChoiceField on my registration form but from the reading and Youtube guide's I have followed, the field that is supposed to be loading the choices is not loading. This is my registration form

How to model a database to a Dynamic HTML Form?

I'm trying to make a POST REQUEST to link Dynamic HTML form (when you push the add button, will add another textbox, like in the photo) into Django's default database. Right now I have the following issue, every time I submit data, it only saves the last row. How is the best way to model database and HTML form to save that data ? Any documentation is appreciate it !
Infraestructura.html
{% extends "Portafolio/layout.html" %}
{% load static %}
{% block scripts %}
<script src = "{% static 'Portafolio/scriptinfra.js' %}" type="text/javascript"></script>
{% endblock %}
{% block content %}
<form action= "{% url 'infraestructura' %}" method="POST" class="form mt-5" id="infra">
{% csrf_token %}
<h1>Factibilidad Técnica y Operativa</h1>
<h2>Análisis de Infraestructura</h2>
<main class="container">
<section class="row">
<div class="col-lg-4 mb-2">
<input name='Infraestructura' class="form-control" type="text" placeholder="Infraestructura">
</div>
<div class="col-lg-4 mb-2">
<input name='Tiempo' class="form-control" type="text" placeholder="Tiempo">
</div>
<div class="col-lg-4 mb-2">
<input name='Costo' class="form-control" type="text" placeholder="Costo Mensual">
</div>
</section>
</main>
<nav class="btn-group">
<button id='add' class='btn btn-success' type='button'>
<i class="fa fa-plus-square"></i> Añadir
</button>
<button id='rem' class='btn btn-danger' type='button'>
<i class="fa fa-minus-square"></i> Eliminar
</button>
</nav>
<!-- Submit buttom-->
<div class="d-grid gap-2 mt-3">
<input type="submit" class="btn btn-lg btn-primary">
</div>
</form>
{% endblock %}
models.py
from django.db import models
# Create your models here.
class Proyecto(models.Model):
NombreProyecto = models.CharField(max_length=64)
ResponsableProyecto = models.CharField(max_length=64)
EvaluadorProyecto = models.CharField(max_length=64)
DescripcionProyecto = models.CharField(max_length=500)
class Financiamiento(models.Model):
MontoProyecto = models.IntegerField(null=True, blank=True)
MontoPropio = models.IntegerField(null=True, blank=True)
MontoBanca = models.IntegerField(null=True, blank=True)
MontoPublico = models.IntegerField(null=True, blank=True)
class Beneficio(models.Model):
Rentabilidad = models.BigIntegerField(null=True, blank=True)
class Infraestructura(models.Model):
Infraestructura= models.CharField(max_length= 64)
Tiempo = models.IntegerField(null=True, blank=True)
Costo = models.BigIntegerField(null=True, blank=True)
class Herramienta(models.Model):
Herramienta= models.CharField(max_length= 64)
Cantidad = models.IntegerField(null=True, blank=True)
Costo = models.BigIntegerField(null=True, blank=True)
class Equipo(models.Model):
Personal = models.CharField(max_length= 64)
Cantidad = models.IntegerField(null=True, blank=True)
Duracion = models.IntegerField(null=True, blank=True)
Costo = models.IntegerField(null=True, blank=True)
class MateriaPrima(models.Model):
MateriaPrima = models.CharField(max_length= 64)
Tiempo = models.IntegerField(null=True, blank=True)
ValorUnitario = models.IntegerField(null=True, blank=True)
views.py
def infraestructura(request):
if request.method =='POST':
infraestructura = request.POST.get('Infraestructura')
tiempo = request.POST.get('Tiempo')
costo = request.POST.get('Costo')
Infraestructura.objects.create(Infraestructura=infraestructura, Tiempo=tiempo, Costo=costo)
return render (request, "Portafolio/Infraestructura.html")
You need to use getlist instead of get and create all the objects with bulk_create
def infraestructura(request):
if request.method =='POST':
infraestructura = request.POST.getlist('Infraestructura')
tiempo = request.POST.getlist('Tiempo')
costo = request.POST.getlist('Costo')
infras_list = []
for i,t,c in zip(infraestructura, tiempo, costo)
infras = Infraestructura(Infraestructura=i, Tiempo=t, Costo=c)
infras_list.append(infras)
Infraestructura.objects.bulk_create(infras_list)
OR you can make use of Django Formsets

Modifying a dropdown menu option in a form Django

I have a dropdown menu that i'm using to fill a field on a form, but, as i'm just starting with django and html, i'm using another form to edit and update the data of the previous form. But i cant make the dropdown menu to work on that second form.
The model:
class Orden(models.Model):
STATUS = (
('En espera', 'En espera'),
('En proceso', 'En proceso'),
('Terminado', 'Terminado'),
('Entregado', 'Entregado'),
)
num_orden = models.CharField(max_length=20, default='')
fechain = models.CharField(max_length=30, default='')
fechaout = models.CharField(max_length=30, default='')
instrumento = models.CharField(max_length=30, default='')
marca = models.CharField(max_length=20)
referencia = models.CharField(max_length=30, default='')
serial = models.CharField(max_length=15, default='')
encargado = models.CharField(max_length=50, default='')
abono = models.CharField(max_length=15, default='')
procesos = models.TextField(default='')
estado = models.CharField(max_length=50, null=True, choices=STATUS) # <--- This one
client = models.ForeignKey(Cliente, on_delete=models.CASCADE, default='', related_name="client")
The view related to the first form:
def ordenfill(request, client_id):
if request.method == 'POST':
orden = ordenForm(request.POST)
if orden.is_valid():
orden.instance.client_id = client_id
init = orden.save()
return redirect('ordenview', id=init.id)
else:
orden = ordenForm()
client = Cliente.objects.get(id=client_id)
context = {
'orden': orden,
'client': client,
}
return render(request, 'ordenfill.html', context)
The template part asociated to that dropdown menu:
<form method="POST" class="post-form" action="{% url 'ordenfill' client.id %}">
{% csrf_token %}
<div class="container">
<br>
.
.
.
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
{{orden.estado}}
</div>
</div>
</div>
</form>
Image of the First Form
Dropdown menu on the first form
The view related to the second form:
def ordenupd(request, id):
orden = Orden.objects.get(id=id)
status = Orden.STATUS
context = {
'orden': orden,
'status': status,
}
return render(request, "ordenupd.html", context)
Edit: the view that saves the changes in the second form
def ordenmod(request, id):
orden = get_object_or_404(Orden, id=id)
if request.method == 'GET':
orden = ordenForm(instance=orden)
return redirect("ordenview", id=id)
else:
orden = ordenForm(request.POST, instance=orden)
print(orden.errors)
if orden.is_valid():
orden.save()
return redirect("ordenview", id=id)
else:
orden = clienteForm()
return redirect("ordenview", id=id)
The template part asociated to the dropdown menu of that view:
<form method="POST" class="post-form" action="/ordenmod/{{orden.id}}">
<input type="hidden" name="id" id="id" required maxlength="20" value="{{orden.id}}"/>
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
<select name="select_path" id="select_path">
<option value="{{orden.estado}}">{{orden.estado}}</option>
{% for items in status %}
<option value ="{{orden.estado}}">{{orden.estado}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</form>
And the webpage of that view:
Dropdown menu on the second form
How can i make that dropdown menu on the second form to work like the one on the first form?
#chnage your view as below
from .models import STATUS
def ordenupd(request, id):
status = STATUS
context = {
'status': status,
}
return render(request, "ordenupd.html", context)
#change your HTML form template as below
<form method="POST" class="post-form" action="/ordenmod/{{orden.id}}">
<input type="hidden" name="id" id="id" required maxlength="20" value="{{orden.id}}"/>
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
<select name="select_path" id="select_path">
{% for items in status %}
<option value ="{{item.1}}">{{item.1}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</form>

how to show data through input tag

I am working on a Django project .In front-end I want to add user data through input tags but I do not know how to save data through input tags
My models.py file is::
class User(AbstractBaseUser):
full_name = models.CharField(max_length=255, blank=True, null=True)
sur_name = models.CharField(max_length=255, blank=True, null=True)
email = models.EmailField(max_length=255 ,unique=True)
choose_subject = models.CharField(choices=SUBJECT_CHOICES , max_length=100)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
time_stamp = models.DateTimeField(auto_now_add=True)
father_name = models.CharField(max_length=255)
father_sur_name = models.CharField(max_length=255)
mobile_phone_number = models.IntegerField(blank=True,null=True)
father_email = models.EmailField(max_length=255, unique=True)
fiscal_code = models.CharField(max_length=20)
address = models.CharField(max_length=200 )
A part of my register.html file is:
<div class="card-body">
<h2 class="title">Registration Form</h2>
<form method="POST">{% csrf_token %}
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label">Full name</label>
<input class="input--style-4" type="text" name="first_name"{{form.full_name}}>
</div>
</div>
<div class="col-2">
<div class="input-group">
<label class="label">Sur name</label>
<input class="input--style-4" type="text" name="last_name"{{form.sur_name}}>
</div>
</div>
</div>
I just want the Django model field to be working in input tags
You must create in the views.py file a method in which the templates and the logic of it, an example can be the following
views.py
#csrf_exempt
def register(request):
if request.method == 'POST':
var1 = request.POST.get('var1')
var2 = request.POST.get('var2')
var3 = request.POST.get('var3')
#Save to the database here
return render_to_response(
'home.html',
{'message': 'Update Success', }
)
else:
#elif request.method == "GET":
obj = table.objects.all()
if obj:
ctx['data'] = obj
return render(request, template_name, ctx)
and in your template add a form tag
<form method="POST">
{ % csrf_token % }
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label">Full name</label>
<input class="input--style-4" type="text" value={{data.full_name}}>
</div>
</div>
<div class="col-2">
<div class="input-group">
<label class="label">Sur name</label>
<input class="input--style-4" type="text" value={{data.sur_name}}>
</div>
</div>
</div>
<input type="submit" value="OK">
</form>
refer this tutorial

Appear data in URL form bu using Django

I want to create a record and want to appear data in the form fields. How can I do that ? Do I need to write javascript for it. If you help me, really apprepriate it. Thanks for now.
Here is models.py;
class hesaplarim(models.Model):
hesap_id = models.AutoField(primary_key=True)
tc_no = models.IntegerField(unique=True)
name = models.CharField(max_length=55)
surname = models.CharField(max_length=55)
phone_number = models.IntegerField()
gender = models.CharField(max_length=5)
Here is views.py;
def home(request):
form = HesapForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = HesapForm()
return render(request, 'home.html', {'form': form})
Here is forms.py;
class HesapForm(forms.ModelForm):
ERKEK = 'ERKEK'
KADIN = 'KADIN'
gender_choices = (
(ERKEK, 'ERKEK'),
(KADIN, 'KADIN')
)
tc_no = forms.IntegerField(widget=forms.NumberInput)
name = forms.CharField(widget=forms.TextInput)
surname = forms.CharField(widget=forms.TextInput)
phone_number = forms.IntegerField(widget=forms.NumberInput)
gender = forms.ChoiceField(choices=gender_choices)
class Meta:
model = hesaplarim
fields = ('tc_no', 'name', 'surname', 'phone_number',
'gender')
Here is html file;
<form method="post" class="form-horizontal" novalidate>
{% csrf_token %}
<div class="form-group">
<label for="id_tc_no">TC No:</label>
{{ form.tc_no }}
</div>
<div class="form-group">
<label for="id_name">Ad:</label>
{{ form.name }}
</div>
<div class="form-group">
<label for="id_surname">Soyad:</label>
{{ form.surname }}
</div>
<div class="form-group">
<label for="id_phone">Cep Telefonu:</label>
{{ form.phone_number }}
</div>
<div class="form-group">
<label for="id_gender">Cinsiyet:</label>
{{ form.gender }}
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Kaydet">
</div>
</form>
You need to initialize the form:
def home(request):
form = HesapForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = HesapForm(initial={'your_field': 'your_value'})
return render(request, 'home.html', {'form': form})
Dynamic initial values you can refer about the same here.
You can also initialize it with object as well.