Django + Python how to update a table item

Asked

Viewed 856 times

3

Hey, guys, I have a registration flow where in a first step save the student and then the data of the responsible, however, after that I need the student to make a description and this description has to be inserted in the respective student, but the way I am not updating anything, but no error returns

this to my py.models :

from django.db import models
from django import forms
from django.forms import ModelForm

class Student(models.Model):

    MARITAL_STATUS_CHOICES = (
        ('casado', 'Casado (a)'),
        ('solteiro', 'Solteiro (a)'),
        ('separado', 'Separado (a)'),
        ('divorciado', 'Divorciado (a)'),
        ('viuvo', 'Viúvo (a)'),
        ('amasiado', 'Amasiado (a)'),
    )

    ETHNIC_GROUP_CHOICES = (
        ('amarelo', 'Amarelo (a)'),
        ('branco', 'Branco (a)'),
        ('indigena', 'Indigena'),
        ('pardo', 'Pardo (a)'),
        ('preto', 'Preto (a)'),
        ('nao declarado', 'Não Declarado (a)'),
    )

    GENDER_CHOICES = (
        ('feminino', 'Feminino'),
        ('masculino', 'Masculino'),
    )

    name = models.CharField(
        max_length=100, verbose_name='Nome')
    rg = models.CharField(
        max_length=14, verbose_name='RG')
    cpf = models.CharField(
        max_length=14, verbose_name='CPF')
    birthDay = models.CharField(
        max_length=10, verbose_name='Data de nascimento')
    adress = models.CharField(
        max_length=100, verbose_name='Endereço')
    email = models.EmailField(
        max_length=100, verbose_name='E-mail')
    phoneNumber = models.CharField(
        max_length=14, verbose_name='Telefone')
    maritalStatus = models.CharField(
        max_length=50, choices=MARITAL_STATUS_CHOICES, verbose_name='Estado civil')
    nationality = models.CharField(
        max_length=50, verbose_name='Nacionalidade')
    naturalness = models.CharField(
        max_length=50, verbose_name='Naturalidade')
    ethnicGroup = models.CharField(
        max_length=50, choices=ETHNIC_GROUP_CHOICES, verbose_name='Grupo Étnico')
    gender = models.CharField(
        max_length=50, choices=GENDER_CHOICES, verbose_name='Gênero')
    description = models.TextField(
        max_length=255, verbose_name='Por que você ?'
    )

    def __str__(self):
        return self.name

class ParentStudent(models.Model):
    name = models.CharField(
        max_length=100, verbose_name='Nome')
    rg = models.CharField(
        max_length=14, verbose_name='RG')
    cpf = models.CharField(
        max_length=14, verbose_name='CPF')
    email = models.EmailField(
        max_length=100, verbose_name='E-mail')
    phoneNumber = models.CharField(
        max_length=14, verbose_name='Telefone')

    def __str__(self):
        return self.name

class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = ['name', 
                'rg', 
                'cpf',
                'birthDay',
                'adress', 
                'email', 
                'phoneNumber', 
                'maritalStatus', 
                'nationality', 
                'naturalness',
                'ethnicGroup',
                'gender',]

class StudentDescriptionForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = ['description']

class ParentStudentForm(forms.ModelForm):
    class Meta:
        model = ParentStudent
        fields = ['name',
                'rg',
                'cpf',
                'email',
                'phoneNumber',]

this is my py views. :

from django.shortcuts import render,  redirect
from .models import Student, StudentForm, StudentDescriptionForm, ParentStudent, ParentStudentForm

def student_form(request):
    if request.method == 'POST':
        form = StudentForm(request.POST)

        if form.is_valid():
            student = Student (**form.cleaned_data)
            student.save()
            return redirect('/register/2')
    else:
        form = StudentForm()

    return render(request, 'student-form-registration.html', {'form': form})

def parent_student_form(request):
    if request.method == 'POST':
        form = ParentStudentForm(request.POST)

        if form.is_valid():
            parent_student = ParentStudent (**form.cleaned_data)
            parent_student.save()
            return redirect('/register/4')
    else:
        form = ParentStudentForm()

    return render(request, 'parent-student-form-registration.html', {'form': form})

def student_description_form(request):
    if request.method == 'PUT':
        form = StudentDescriptionForm(request.PUT)

        if form.is_valid():
            student = Student (**form.cleaned_data)
            student.save()
            return redirect('/register')
    else:
        form = StudentDescriptionForm()
    
    return render(request, 'student-description-form.html', {'form': form})

and this is mine student-Description-form.html :

{%extends "base.html"%}
{% load bootstrap4 %}

{%block student_description%}
<form method="PUT">
    {% csrf_token %} 
    {% bootstrap_form form %}
    <button type="submit" class="btn btn-primary mb-4">Avançar</button>
</form>
{%endblock%}

1 answer

0

Enough in your view update the object in the database with Model.refresh_from_db follows a link to the documentation refresh_from_db.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.