Home > database >  django put save button
django put save button

Time:09-05

I would like to change the data that is in this column from a form but I can't display the data and save them and also add a delete button. anyone have a solution.

models.py

class Employe(models.Model):
Matricule = models.CharField(max_length=10, null=False)
Prenom = models.CharField(max_length=40, null=True)
Nom = models.CharField(max_length=40, null=True)
Tel = models.CharField(max_length=20, null=True)
Adresse = models.CharField(max_length=100, null=True)
Courriel = models.CharField(max_length=100, null=True)
Horaire = models.CharField(max_length=50, null=True)
Date_embauche = models.CharField(max_length=100, null=True)
UtilisateurOSP = models.CharField(max_length=100, null=True)
MotdepasseOSP = models.CharField(max_length=50, null=True)
Anciennete = models.CharField(max_length=10, null=True)
data_created = models.DateTimeField(auto_now_add=True, null=True)

class Disciplinaire(models.Model):
employe = models.ForeignKey(Employe, null=True, on_delete=models.SET_NULL)
Avis_verbal = models.CharField(max_length=300, null=True)
Avis_ecrit = models.CharField(max_length=300, null=True)
Avis_ecrit2 = models.CharField(max_length=300, null=True)
Suspension = models.CharField(max_length=300, null=True)
Fin_emploie = models.CharField(max_length=300, null=True)
data_created = models.DateTimeField(auto_now_add=True, null=True)

forms.py

class disciplinaireForm(ModelForm):
class Meta:
    model = Disciplinaire
    fields = '__all__'

views.py

def updateDisciplinaire(request, id):
emp = Disciplinaire.filter()
forms = disciplinaireForm(instance=emp)

if request.method == 'POST':
    forms = disciplinaireForm(request.POST, instance=emp)
    if forms.is_valid():
        forms.save()
        return redirect('/')

context = {'forms':forms}
return render(request, 'accounts/updateDisciplinaire.html', context)

page.html

      <th scope="col">Avis Verbal</th>
      <th scope="col">1er Avis Écrit</th>
      <th scope="col">2e Avis Écrit</th>
      <th scope="col">Suspension</th>
      <th scope="col">Fin d'emploi</th>
      <th scope="col">Action</th>
      </tr>
     {% for j in disci %}
      <tr>
        <td>{{j.Avis_verbal}}</td>
        <td>{{j.Avis_ecrit}}</td>
        <td>{{j.Avis_ecrit2}}</td>
        <td>{{j.Suspension}}</td>
        <td>{{j.Fin_emploie}}</td>
        <td><a  href="{% url 'updateDisciplinaire' j.id %}">Modifier</a>
      </tr>
    {% endfor %}

data and button

error message

CodePudding user response:

The error says that in line 102 from your views.py you are trying to filter something. If you want to filter try:

emp_all = Disciplinaire.objects.filter(employe = id)
for emp in emp_all:
  print(emp.Matricule)

you need to put how to filter, by which field.
When you use filter it will get you a queryset, a list of records not just one single record, if you want a single record you can use get:

emp = Disciplinaire.objects.get(employe = id)

CodePudding user response:

page.html

<h5 >Disciplinaire</h5>
<div >
<div >
<div >
  <table >
    <tr>
      <th scope="col">Avis Verbal</th>
      <th scope="col">1er Avis Écrit</th>
      <th scope="col">2e Avis Écrit</th>
      <th scope="col">Suspension</th>
      <th scope="col">Fin d'emploi</th>
      <th scope="col">Action</th>
      </tr>
     {% for j in disci %}
      <tr>
        <td>{{j.Avis_verbal}}</td>
        <td>{{j.Avis_ecrit}}</td>
        <td>{{j.Avis_ecrit2}}</td>
        <td>{{j.Suspension}}</td>
        <td>{{j.Fin_emploie}}</td>
        <td><a  href="{% url 'updateDisciplinaire' j.id %}">Modifier</a>
      </tr>
    {% endfor %}

views.py

def updateDisciplinaire(request, id):
emp = Disciplinaire.objects.get(employe = id)   
forms = disciplinaireForm(instance=emp)

if request.method == 'POST':
    forms = disciplinaireForm(request.POST, instance=emp)
    if forms.is_valid():
        forms.save()
        return redirect('/')

context = {'forms':forms, 'emp':emp}
return render(request, 'accounts/updateDisciplinaire.html', context)

When i click on modified button i want to have the information in my form and change it if necessary

  • Related