DJANGO:尝试将POST数据保存到学生模型 - IntegrityError

时间:2018-03-25 06:41:45

标签: python django django-models django-forms

您好我正在尝试将数据从表单保存到学生模型;摘要用户模型。

请帮忙!

  

问题:/ register / UNIQUE约束中的IntegrityError失败:   accounts_students.username

不确定是什么问题,在视图中我试图清理数据并将其保存到数据库中。

  • 有趣的是,数据正在保存,只是页面没有 重定向!

    from .models import *
    
    from django.contrib.auth.forms import UserCreationForm, UserChangeForm
    
    from betterforms.multiform import MultiModelForm
    from django.contrib.auth.forms import UserCreationForm
    
     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
    

models.py

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)

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

# Create your views here.
# def login(request):
#       return render(request, 'accounts/login.html')

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():

            bio = form.cleaned_data['bio']
            location = form.cleaned_data['location']
            birth_date = form.cleaned_data['birth_date']
            form.save()

            Students.objects.create(bio=bio, location=location, birth_date=birth_date)
            # 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)

0 个答案:

没有答案
相关问题