2015-01-07 7 views
0

내가 등록 양식을 사용자 등록이 자동으로 로그인 할 때 나는 함수 전망을 얻을 수 필요하지만 난 클래스를 기반으로 볼 수있는 때 그것은하지함수보기에 등록한 후에 클래스 기반보기가 아닌 로그인을 할 수없는 이유는 무엇입니까?

내 기능도 작동 않습니다

def register(request): 
    if request.method=="POST": 
     form = UserCreateForm(request.POST) 
     if form.is_valid(): 
      new_user = form.save() 
      new_user = authenticate(username=request.POST['username'],password=request.POST['password1']) 
      login(request, new_user) 
      return HttpResponseRedirect("/contadores/index") 
    else: 
     form = UserCreateForm() 
    return render_to_response("contadores/register.html", { 
     'form': form, 
    }, context_instance=RequestContext(request)) 

내 클래스를 기반으로보기 : 분명히

class RegisterView(FormView): 
    form_class = UserCreateForm 
    template_name = 'contadores/register.html' 
    success_url = '/contadores/create/' 

    def form_valid(self,form): 
     user = form.save() 
     user = authenticate(username=self.request.POST['username'],password=self.request.POST['password']) 
     login(request,user) 
     return super(RegisterView,self).form_valid(form) 

    def dispatch(self, request, *args, **kwargs): 
     if request.user.is_authenticated(): 
      return HttpResponseRedirect('/contadores/create') 
     else: 
      return super(RegisterView, self).dispatch(request, *args, **kwargs) 

내가 클래스를 기반으로보기 및 url 및 그 반대의 경우도 마찬가지, 내가 클래스 기반의 뷰가 함께있어 오류를 비활성화 기능보기로 시도 :

예외 유형 : MultiValueDictKeyError 예외 값 : " '암호'"

내 클래스를 기반으로보기에 어떤 문제가 있습니까?

답변

0

POST dict에서 .get을 사용하면 나타나는 오류를 회피 할 수 있습니다.

self.request.POST.get('password', '') 

액세스하려는 키가 또한 그 예외를 제기하지 않을 것이다 발견되지 않는 경우 디폴트 값을 제공 할 수있게된다 .get 사용.

두 가지 다른 암호 필드가있는 것 외에도 기능 기반보기에는 self.request.POST['password1']이 있고 CBV에는 self.request.POST['password']이 있습니다.