2016-02-11 42 views
0

在django 1.9中,如何在注册后自动登录用户?注册后自动登录

我遵循this question的回答,但在试用时得到this error

我加AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)到的settings.py

这里是我的views.py:

from django.shortcuts import render 
from django.views.generic.edit import CreateView, UpdateView, DeleteView 
from .models import UserProfile 
from django.contrib.auth.models import User 
from .forms import UserForm 
from .forms import UserProfileForm 
from django.http import HttpResponseRedirect 
from django.contrib.auth import authenticate, logout, login 

# Create your views here. 
def RegisterView(request): 

    registration_success = False 

    if request.method == 'POST': 
     user_form = UserForm(request.POST, prefix='uf') 
     profile_form = UserProfileForm(request.POST, request.FILES, prefix='upf') 

     if user_form.is_valid() and profile_form.is_valid(): 
      user_atr = user_form.save() 
      user_atr.set_password(user_atr.password) 
      user_atr.save() 

      profile = profile_form.save(commit=False) 
      profile.user = user_atr 

      profile.avatar = request.FILES.get('avatar') 

      profile.save() 
      registration_success = True 
      user_atr = authenticate(username=request.POST['username'], 
            password=request.POST['password']) 
      login(request, user_atr) 
      return HttpResponseRedirect("/") 
     else: 
      print (user_form.errors, profile_form.errors) 

    else: 
     user_form = UserForm(prefix='uf') 
     profile_form = UserProfileForm(prefix='upf') 

    return render(request, 'profile/register.html', {'profile_form': profile_form, 'user_form': user_form}) 

而且我forms.py:

class UserForm(ModelForm): 

    def __init__(self, *args, **kwargs): 
     super(UserForm, self).__init__(*args, **kwargs) 
     for field_name, field in self.fields.items(): 
      field.widget.attrs['class'] = 'field-input-element' 

    confirm_password = forms.CharField(widget=forms.PasswordInput()) 

    class Meta: 
     model = User 
     fields = ('username', 'email', 'password') 
     widgets = { 
      'password': forms.PasswordInput(), 
     } 


class UserProfileForm(ModelForm): 

    def __init__(self, *args, **kwargs): 
     super(UserProfileForm, self).__init__(*args, **kwargs) 
     for field_name, field in self.fields.items(): 
      field.widget.attrs['class'] = 'field-input-element' 

    class Meta: 
     model = UserProfile 
     fields = ('display_name', 'avatar', 'birthday', 'usertype', 'daw', 'usergenre', 'gender') 
     widgets = { 
      'birthday': AdminDateWidget(), 
     } 
+0

尝试'request.POST.get('username')'和'request.POST.get('password')'代替。 – rinti

+0

@rinti - 这将防止错误,但不会帮助,因为它会尝试使用'user = None'和'password = None'进行身份验证。 – Alasdair

+0

您不需要在您的设置中设置'AUTHENTICATION_BACKENDS',因为您正在使用默认值。 – Alasdair

回答

2

您正在使用您的形式前缀,因此数据将发布为uf-usernameuf-password。因此request.POST['username']给出KeyError

无论如何,直接从request.POST获取数据通常不是一个好主意。取而代之的是从表单的清理数据中取出它,然后让Django处理前缀和验证。

user_atr = authenticate(username=user_form.cleaned_data['username'], 
         password=user_form.cleaned_data['password'], 
         )