Django + Postgresql object has no attribute

0

I'm trying to do a validation before sending my form, however it returns me the following error: 'StudentForm' object has no termAccepted attribute

app models.py:

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

class Student(models.Model):

    TAKE_COMPUTER_CHOICES = (
        ('sim', 'Sim'),
        ('nao', 'Não'),
    )
    COMPUTER_TYPE_CHOICES = (
        ('windows', 'Windows'),
        ('linux', 'Linux'),
        ('mac', 'Mac'),
    )

    cpf = models.CharField(
        max_length=14, unique = True, verbose_name = 'CPF')
    ra = models.CharField(
        max_length=10, unique= True, verbose_name='RA')
    email = models.EmailField(
        max_length=100, verbose_name='E-mail')
    takeComputer = models.CharField(
        max_length=3, choices=TAKE_COMPUTER_CHOICES, verbose_name='Você vai trazer seu notebook?:')
    computerType = models.CharField(
        max_length=7, choices=COMPUTER_TYPE_CHOICES, verbose_name='Qual o sistema operacional do seu notebook?:')

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    termAccepted = models.BooleanField(default=1, verbose_name='Eu li e aceito o uso da minha imagem')

    def __str__(self):
        return self.email

class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = ['cpf',
                'ra',
                'email',
                'termAccepted',
                'takeComputer',
                'computerType',]

app views.py:

from django.shortcuts import render, redirect
from .models import *

def create_student(request):
    form = StudentForm(request.POST or None)

    if form.is_valid() and form.termAccepted == True:
        student = form.save()
        request.session['student_id'] = student.id
        return redirect('registrations:create_student')

    return render(request, 'student-form-registration.html', {'form': form})
    
asked by anonymous 28.08.2018 / 21:07

1 answer

1

You will have access to form data (once validated) through the .cleaned_data attribute. Then it would look something like this:

 if form.is_valid() and form.cleaned_data['termAccepted'] == True:
        student = form.save()
        request.session['student_id'] = student.id
        return redirect('registrations:create_student')
    
29.08.2018 / 01:44