注册表表单未保存到数据库:Django?

2024-10-03 17:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

登记表上有奇怪的问题,我不确定我做错了什么。

我有StudentProfile模型,我试图从StudentResistrationForm保存数据,但是数据没有保存到数据库中

ERROR: NameError at /register/ name 'StudentProfile' is not defined

视图逻辑是否正确?我错过了什么?想法请

型号

class Accounts(AbstractUser):
    email = models.EmailField('email address', unique=True)
    first_name = models.CharField('first name', max_length=30, blank=True)
    last_name = models.CharField('last name', max_length=30, blank=True)
    date_joined = models.DateTimeField('date joined', auto_now_add=True)

    # asdd
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)


class StudentProfile(models.Model):
    user = models.OneToOneField('Accounts', related_name='student_profile')
    # additional fields for students
    AMEB_Ratings = models.PositiveIntegerField(default=0)
    is_student = models.BooleanField('student status', default=False)

形式

class StudentResistrationForm(forms.ModelForm):
    class Meta:
        model = StudentProfile
        fields = (  
            'AMEB_Ratings',

        )

    def save(self, commit=True):
        user = super(StudentResistrationForm, self).save(commit=False)
        # user.first_name = self.cleaned_data['first_name']
        # user.last_name = self.cleaned_data['last_name']
        user.AMEB_Ratings = self.cleaned_data['AMEB_Ratings']

        if commit:
            user.save()

        return user 

    class UserForm(forms.ModelForm):
        class Meta:
            model = get_user_model()
            fields = ('username', 'email', 'password')

查看

def registerStudent(request):
    # Once register page loads, either it will send to the server POST data (if the form is submitted), else if it don't send post data create a user form to register
    if request.method == "POST":
        user_form = UserForm(request.POST)
        form = StudentResistrationForm(request.POST)

        if form.is_valid() and user_form.is_valid():
            User = get_user_model()
            username = user_form.cleaned_data['username']
            email = user_form.cleaned_data['email']
            password = user_form.cleaned_data['password']
            new_user = User.objects.create_user(username=username, email=email, password=password)

            Student_profile = StudentProfile()
            Student_profile.user = new_user
            Student_profile.AMEB_Ratings = request.POST['AMEB_Ratings']
            # Student_profile = StudentProfile.create_user(AMEB_Ratings=AMEB_Ratings)
            new_user.save()
            Student_profile.save()
            # form.save()

            # AMEB_Ratings = form.cleaned_data['AMEB_Ratings']
            return redirect('/')
    else:
        #  Create the django default user form and send it as a dictionary in args to the reg_form.html page.
        user_form = UserForm()
        form = StudentResistrationForm()        

        # args = {'form_student': form, 'user_form': user_form }
    return render(request, 'accounts/reg_form_students.html', {'form_student': form, 'user_form': user_form })

Tags: nameformtruedatamodelsemailrequestsave
1条回答
网友
1楼 · 发布于 2024-10-03 17:26:51

看起来您有一些输入错误您目前正在设置您的电子邮件变量的电子邮件数据,然后设置它的密码数据。先纠正这个。你知道吗

email = user_form.cleaned_data['email'] password = user_form.cleaned_data['password']

相关问题 更多 >