NameError:'user'未定义:尝试扩展抽象用户模型 - DJANGO

时间:2018-03-25 04:51:20

标签: python django abstract

首次尝试通过扩展用户模型来创建学生用户。

  

问题:点击注册btn ieLogin(btn)代替   重定向到home它显示以下内容:NameError at / register /   ...名称'用户'未定义

     

文件“E:\ ifb299 \ tutorial2 \ accounts \ views.py”,第33行,在寄存器中       Students.objects.create(user = user)NameError:未定义名称'user'[25 / Mar / 2018 14:38:07]“POST / register / HTTP / 1.1”500 67801

不确定我做错了什么,为什么Students.objects.create(用户=用户)错了,我该如何解决呢?

views.py

from django.shortcuts import render

from django.shortcuts import redirect

from accounts.forms import RegistrationForm, EditProfileForm

from django.contrib.auth.models import User

from accounts.models import Students

from django.contrib.auth.forms import UserChangeForm

from django.http import HttpResponse

from django.contrib.auth.decorators import login_required

def home(request):
    return render(request, 'accounts/home.html')

def login_redirect(request):
    return redirect('/login/')

def register(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":
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            Students.objects.create(user=user)
            return redirect('../home/')
    else:
        #  Create the django default user form and send it as a dictionary in args to the reg_form.html page.
        form = RegistrationForm()
    args = {'form': form}
    return render(request, 'accounts/reg_form.html', args)

@login_required 
def view_profile(request):
    args = {'user': request.user}
    return render(request, 'accounts/profile.html', args)

@login_required 
def edit_profile(request):
     # Handle post request - if the user submits a form change form details and pass the intance user
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect('../profile')
    # Handles the get request - if no post info is submitted then get the form and display it on the edit profile page. 
    else:
        form = EditProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'accounts/profile_edit.html', args)

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.db.models.signals import *
from django.conf import settings

class Students(AbstractUser):
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

forms.py

from django import forms
from django.contrib.auth.models import User


from .models import *
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from betterforms.multiform import MultiModelForm
from django.contrib.auth.forms import UserCreationForm

# Create a custom form that inherites form UserCreationForm (adding our own fields to save i db)
# Inheriting form in the paramters ()
class RegistrationForm(UserCreationForm): 
    email = forms.EmailField(required=True)

    class Meta:
        model = Students
        fields = (
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',
            'bio',
            'location',
            'birth_date',

        )   

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

        if commit:
            user.save()

        return user

#  Inherits from UserChangeForm class - we keep everything i.e. methods, functionality same but change the things we want to show - connected to the User model 
class EditProfileForm(UserChangeForm):

    class Meta:
        model = User
        # Create fields variable get has all the fields we want to show 
        fields = (
            'email',
            'first_name',
            'last_name',
            'password'
        )

1 个答案:

答案 0 :(得分:0)

首先,您没有将form.save()的返回值保存到变量user

第二,您的模型user没有字段Student