Hello the I need to edit and update the already existing row of data in the database.I guess that their is some problem in views.py. So I am not sure my views.py is right. Please check and tell me if any problem and how to proceed.
Thanks
models.py is :
class chefequipe(models.Model):
nom = models.CharField(max_length=200)
prenom = models.CharField(max_length=200)
matricule = models.CharField(max_length=200)
login = models.CharField(max_length=200)
password = models.CharField(max_length=200)
statut = models.CharField(max_length=200)
objects = models.Manager()
def __str__(self):
return self.matricule
the forms.py :
class ChefEquipeRegister(forms.Form):
nom= forms.CharField(required=True , widget=forms.TextInput)
prenom=forms.CharField(required=True , widget=forms.TextInput)
matricule=forms.CharField(required=True , widget=forms.TextInput)
login=forms.CharField(required=True , widget=forms.TextInput)
password=forms.CharField(required=True , widget=forms.PasswordInput)
statut=forms.CharField(required=True , widget=forms.TextInput)
the view.py
def updatechef(request , id):
var=models.chefequipe.objects.filter(id=id)
form_data = forms.ChefEquipeRegister(request.POST or None )
if form_data.is_valid():
chefequipe = models.chefequipe()
chefequipe.nom = form_data.cleaned_data['nom']
chefequipe.prenom= form_data.cleaned_data['prenom']
chefequipe.login = form_data.cleaned_data['login']
chefequipe.password = form_data.cleaned_data['password']
chefequipe.statut = form_data.cleaned_data['statut']
chefequipe.matricule = form_data.cleaned_data['matricule']
chefequipe.save()
context= {
'var' : var,
}
return render(request , 'administrateur/updatechef.html', context )
the page html
{% extends 'baseadmin.html' %}
{% block content %}
<form action="" method="post">
<div class="container">
<div class="columns is-mobile">
<div class="column is-half is-offset-one-quarter">
<form action="" method="post">
{% csrf_token %}
{% for var in var %}
<div class="content"> <h1> Modifier Information Chef D'équipe : {{var.prenom }} {{ var.nom }} </h1></div>
<label>Nom</label> <input id="nom" type="text" name="nom" value="{{ var.nom }}">
<label>Prenom</label> <input id="prenom" type="text" name="prenom" value="{{ var.prenom }}">
<label>Matricule</label> <input id="matricule" type="text" name="matricule" value="{{ var.matricule }}">
<label>Statut</label> <input id="statut" type="text" name="statut" value="{{ var.statut }}">
<label>Login</label> <input id="login" type="text" name="login" value="{{ var.login }}">
<label>Password</label> <input id="password" type="text" name="password" value="{{ var.password }}">
{% endfor %}
</div>
<input type="submit" value="Modifier" class="button is-primary">
</form>
</div>
</div>
</div>
{% endblock %}
i need to update the data exsting in my database