简体   繁体   中英

cleaned_data not working in django 1.8.6

I am working on a django tutorials, and try to validate the forms using cleaned_data properties.

the code of the forms.py file is

 from django import forms
 from .models import SignUp

 class SignUpForm(forms.ModelForm):

        class Meta:
            model = SignUp
            fields = ['email']

        def clean_email(self):
            email = self.cleaned_data.get('email')
            if not '.edu' in email: 
                raise forms.ValidationError('Please use a valid academic email address')
            print(email)
            return email

The models.py file contain the following code

from django.db import models

# Create your models here.
class SignUp(models.Model):
    email = models.EmailField()
    full_name = models.CharField(max_length=255)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return self.email

and the admin.py has the following line of code

from django.contrib import admin
from .models import SignUp
from .forms import SignUpForm

class SignUpAdmin(admin.ModelAdmin):
    list_display = ['full_name', 'email', 'timestamp', 'updated']
    class Meta:
        form = SignUpForm

admin.site.register(SignUp, SignUpAdmin)

The problem what I am getting is, cannot grab the email field and cannot process the email field,infact the cleaned_data contains no data, the print(email) not printing the email in the console. and I cannot validate to the .edu email address

I am using python3 and django1.8.6

You have defined your admin class wrong, so it is not using your custom form. You don't use a class Meta inside a modeladmin; you just define the class attribute directly.

class SignUpAdmin(admin.ModelAdmin):
    list_display = ['full_name', 'email', 'timestamp', 'updated']
    form = SignUpForm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM