My project has 4 forms in which i have a back,next and save button at the bottom. Everytime I press next or save button, a new row is added in database of the same data. How can I avoid that and can edit/update the same row when I changed the data in form.
admission is 1st form
personalinfo is 2nd form
academic is 3rd form
achievements is the 4th form
Here is my views.py
I am only pasting the code for 1st form as rest all are the same
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from usersite.forms import admissionForm,personalinfoForm,academicForm
from usersite.models import admission as admission2
from usersite.models import personalinfo as personalinfo2
from usersite.models import academic as academic2
from django.contrib.auth.models import User
@login_required
def admission(request):
if request.user.is_authenticated:
user = request.user
print(user)
form = admissionForm()
#admission1 = admission2.objects.get(user=2)
try:
admission1 = admission2.objects.filter(user=user).latest('pk')
except:
admission1 = admission2.objects.filter(user=user)
#admission1 = admission2.objects.all()
return render(request,'admission.html', context={'form': form , 'admission1': admission1})
#return render(request,'admission.html', context={'form': form)
else:
return redirect('unauthorised')
@login_required
def submit_admission(request):
if request.user.is_authenticated:
user = request.user
print(user)
form = admissionForm(request.POST)
if form.is_valid():
print("changed")
#print(form.cleaned_data)
admission = form.save(commit=False)
admission.user = user
admission.save()
return redirect("personalinfo")
else:
return render(request,'admission.html',context = {'form': form})
admission.html
<DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Form 1</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/static/css/usersite/admission.css">
</head>
<body>
<div class="container">
<form action="submit_admission/" method="post">
{% csrf_token %}
<div class="nav_log">
<a href="{%url 'logout'%}"><button type="button" class="btn btn-dark">Logout</button></a>
</div>
<h4 style="padding-top: 20px; text-align: center; text-transform: uppercase;">Department of</h4>
<div id="div1">
<h4 id="branch" class="branchselect" style="text-align:center; text-transform:uppercase">
<select id="dropdown" name="branch"
style="background:transparent; border:2px solid transparent;">
<option value="default" selected>Your Branch</option>
<option value="CS">CS</option>
<option value="IT">INFT</option>
<option value="EXTC">EXTC</option>
<option value="AIDS">AI & DS</option>
<option value="Mechanical">Mechanical</option>
<option value="Civil">Civil</option>
</select>
</h4>
</div>
<br>
<h3 style="text-align: center; text-transform: uppercase; text-decoration: underline;">Proctor
Record</h3>
</div>
<br> <br>
<div class="container">
<h5>Name of the Proctor : <input type="text" name="procname" value="{{admission1.procname}}"> </h5>
{{n}}
<br>
<table>
<tbody>
<tr>
<td colspan="3">Surname : <input id="surname" type="text" value="{{admission1.surname}}"
name="surname"></td>
<td colspan="3">First Name : <input id="name" type="text" value="{{admission1.name}}"
name="name"></td>
<td colspan="3">Middle Name : <input id="midname" type="text" value="{{admission1.midname}}"
name="midname"></td>
</tr>
<tr>
<td>Roll No.</td>
<td><input id="rollno1" type="number" placeholder="SEM 1" name="rollsem1"
value="{{admission1.rollsem1}}" onkeypress='validate(event)'></td>
<td><input id="rollno2" type="number" placeholder="SEM 2" name="rollsem2"
value="{{admission1.rollsem2}}" onkeypress='validate(event)'></td>
<td><input id="rollno3" type="number" placeholder="SEM 3" name="rollsem3"
value="{{admission1.rollsem3}}" onkeypress='validate(event)'></td>
<td><input id="rollno4" type="number" placeholder="SEM 4" name="rollsem4"
value="{{admission1.rollsem4}}" onkeypress='validate(event)'></td>
<td><input id="rollno5" type="number" placeholder="SEM 5" name="rollsem5"
value="{{admission1.rollsem5}}" onkeypress='validate(event)'></td>
<td><input id="rollno6" type="number" placeholder="SEM 6" name="rollsem6"
value="{{admission1.rollsem6}}" onkeypress='validate(event)'></td>
<td><input id="rollno7" type="number" placeholder="SEM 7" name="rollsem7"
value="{{admission1.rollsem7}}" onkeypress='validate(event)'></td>
<td><input id="rollno8" type="number" placeholder="SEM 8" name="rollsem8"
value="{{admission1.rollsem8}}" onkeypress='validate(event)'></td>
</tr>
<tr id="photo">
<td colspan="2">Upload your Photo</td>
<td colspan="7">
<input type="file" id="myFile" name="filename">
<a href="https://drive.google.com/drive/u/1/folders/1rmO_Xz9AAcfRy3q6Ca6WmHEh984pSppi">
<input id="btn" type="button" value="Upload" class="btn btn-primary">
</a>
</td>
</tr>
</tbody>
</table>
</div>
<br>
<div class="container">
<br>
<ul style="margin-left: -2%;">
<li>
<h4>Admission Details:</h4>
</li>
</ul>
</div>
<br>
<div class="container">
<table class="adm">
<tr>
<td>Year of Admission</td>
<td><input type="text" value="{{admission1.year}}" name="year" onkeypress='validate(event)'>
</td>
<td>Category of Admission</td>
<td><input type="text" value="{{admission1.category}}" name="category"></td>
<td>HSC (%)</td>
<td><input type="text" value="{{admission1.hsc}}" name="hsc" onkeypress='validate(event)'></td>
</tr>
<tr>
<td>CET Score</td>
<td><input type="text" value="{{admission1.cet}}" name="cet" onkeypress='validate(event)'></td>
<td>JEE Score</td>
<td><input type="text" value="{{admission1.jee}}" name="jee" onkeypress='validate(event)'></td>
<td>Diploma (%)</td>
<td><input type="text" value="{{admission1.diploma}}" name="diploma"
onkeypress='validate(event)'></td>
</tr>
</table>
</div> <br> <br>
<div class="container" id="bottombtns">
<a href="{%url 'personalinfo'%}">
<input class="btn btn-primary submit-btn" id="next" type="submit" value="Next" style="float: right;"
min="0" max="500000000000">
</a>
</div>
<div class="container" id="bottombtns">
<a href="{%url 'admission'%}">
<input class="btn btn-primary submit-btn" type="submit" value="Save" style="float:right; "
id="save">
</a>
</div>
</form>
<div style="padding-bottom: 100px;">
</div>
<div class="html2pdf__page-break"></div>
<script src="static/js/usersite/keychecker.js"></script>
</body>
</html>
models.py
from django.db import models
from django.contrib.auth.models import User
from phonenumber_field.modelfields import PhoneNumberField
from datetime import datetime
class admission(models.Model):
procname = models.CharField(max_length=45, blank=True, null=True)
surname = models.CharField(max_length=45, blank=True, null=True)
name = models.CharField(max_length=45, blank=True, null=True)
midname = models.CharField(max_length=45, blank=True, null=True)
student_image = models.ImageField(upload_to='users/%Y/%m/%d/', blank=True)
rollsem1 = models.CharField(max_length=3, blank=True, null=True)
rollsem2 = models.CharField(max_length=3, blank=True, null=True)
rollsem3 = models.CharField(max_length=3, blank=True, null=True)
rollsem4 = models.CharField(max_length=3, blank=True, null=True)
rollsem5 = models.CharField(max_length=3, blank=True, null=True)
rollsem6 = models.CharField(max_length=3, blank=True, null=True)
rollsem7 = models.CharField(max_length=3, blank=True, null=True)
rollsem8 = models.CharField(max_length=3, blank=True, null=True)
year = models.CharField(max_length=45, blank=True, null=True)
category = models.CharField(max_length=45, blank=True, null=True)
hsc = models.CharField(max_length=45, blank=True, null=True)
cet = models.CharField(max_length=45, blank=True, null=True)
jee = models.CharField(max_length=45, blank=True, null=True)
diploma = models.CharField(max_length=45, blank=True, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
urls.py
from django.urls import path
from.import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admission', views.admission, name='admission'),
path('submit_admission/', views.submit_admission, name='submit_admission'),
path('academic', views.academic, name='academic'),
path('achievementdetails', views.achievementdetails, name='achievementdetails'),
path('personalinfo', views.personalinfo, name='personalinfo'),
path('unauth', views.unauthorised, name='unauth'),
path('logout', views.logout_view, name='logout'),
path('logreg/login/', auth_views.LoginView.as_view()),
path('submit_personalinfo/', views.submit_personalinfo, name='submit_personalinfo'),
path('submit_academic/', views.submit_academic, name='submit_academic'),
]
you want to create a "wizard form with steps". before 1.8 version this was a part of Django, right now it is standalone project. more here: https://docs.djangoproject.com/en/1.8/releases/1.8/#removal-of-django-contrib-formtools form-tools here: https://github.com/jazzband/django-formtools
Short words: you can save step data without to save instance on every step. on the last step you can Save the whole data once time.
other possibility - on every next step you should set admission.pk from saved instance from step before
I see in your answer, you don't understand the idea of ModelForm
, i hope your admissionForm
is modelform.
in your case:
def submit_admission(request):
if request.user.is_authenticated:
user = request.user
admission = Admission.objects.filter(user=request.user).last()
form = admissionForm(request.POST, instance=admission or Admission())
... # other staff
Your form should have instance before save.
Please try to read about ModelForm here: https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#modelform
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.