Reputation: 85
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
Upvotes: 2
Views: 5741
Reputation: 21
In your case:
views.py
.....
.....
.....
if form.is_valid():
object_chefequipe = chefequipe.objects.get(id=id)
form = ChefEquipeRegister(request.POST, instance=object_chefequipe)
form.save()
Upvotes: 1
Reputation: 16
Is there any reason why you are using a Form instead of a ModelForm? ModelForms are used to create or update model instances. Then when you get the request.POST data, you can assign the instance of the model (the database row) that you want to update in the ModelForm.
models.py
class ChefEquipeRegister(forms.ModelForm) ...
views.py
if request.method == POST:
form = ChefEquipeRegister(request.POST, instance=**INSTANCE)
I think that you should make sure your Model has a primary key to access each model instance and then rewrite your Form as a ModelForm. Follow along here: https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/
Upvotes: 0