2012-09-13 2 views
0

이 등록보기에서 인증이 작동하지 않는 이유는 없습니다. 나는 사용자가 데이터베이스에 입력되고 다음 원하는 페이지 (login_required 데코레이터가 있음)로 리다이렉트하고 있지만 다시 로그인 페이지로 리디렉션하기 때문에 그렇게 말합니다. Authenticate는 지정된 값으로 셸에서 새 사용자를 반환합니다. 디버깅하여 붙여 넣었습니다. http://dpaste.org/J1HZ5/무엇이 잘못 되었나요?Auth registration headache

def register(request): 
    if request.method == 'POST': 
     form = UserCreationForm(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) 
      context = RequestContext(request)  
      context['user_id'] = new_user.id 
      context['new_user'] = new_user 
      url = '/user/%s/' % new_user.id 
      return HttpResponseRedirect(url) 
    else: 
     form = UserCreationForm() 
    return render_to_response("registration/register.html", {'form': form}, 
           context_instance=RequestContext(request)) 

urls.py :

urlpatterns = patterns('', 
    # Examples: 
    url(r'^$', 'crewcal.views.index', name='home'), 
    # url(r'^ssc/', include('ssc.foo.urls')), 

    url(r'^events/(?P<event_id>\d+)/$', 'crewcal.views.event'), 
    url(r'^events/new/$', 'crewcal.views.new_event', name='new_event'), 
    url(r'^commit/$', 'crewcal.views.commit'), 
    url(r'^user/(?P<user_id>\d+)/$', user, name='user'), 
    url(r'^users/$', 'crewcal.views.users'), 

    url(r'^register/$', register, name='join'), 
    url(r'^login/$', 'django.contrib.auth.views.login'), 
    url(r'^logout/$', logout, {'next_page': '/'}, name='logout'), 
    url(r'^log_in/$', log_in, name='log_in'), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)), 
) 

감사

+0

업데이트와 질문은' –

+0

urls.py' 그것은 거기 지금에 있습니다. 감사. – KindOfGuy

답변

0

당신이 장고 세션 미들웨어를 설치 했습니까?

+0

세션 미들웨어는 기본적으로 사용됩니다. –

+0

예, 활성화되어 있습니다. – KindOfGuy

0

UserCreationForm 코드를 게시하고 싶지만 authenticate(...) 호출을 실행 중이지만 실제로 결과를 확인한 다음 리디렉션하지 않는 것이 좋습니다. Django 소스를 보면 인증 양식 (django.contrib.forms.AuthenticationForm)이 내장되어 있으며 authenticate() 호출이 none이 아니며 사용자가 활성화되어 있고 그렇지 않은 경우 양식 오류가 발생하는 경우 그 결과를 확인합니다.

+0

사실입니다. 그런데 왜 내가 그렇게해야합니까? 사용자 작성 양식이 올 Y 르게 유효성을 검증하는 경우, 인증 될 사용자 정보는 신선하고 순수해야하며 반드시 실패하지 않아야합니다. 어쨌든, 나는 그것이 (데이터베이스와 쉘 테스트에서) 올바르다는 것을 알고 있지만 아직 인증을하지 못하고있다. 추신. UserCreationForm은 기본값 인'from django.contrib.auth.forms import UserCreationForm'입니다. 그래도 고마워. – KindOfGuy

0

귀하의 문제는이 라인이다 :

url(r'^user/(?P<user_id>\d+)/$', user, name='user'), 

user가 유효한보기 있는지 확인합니다.

+0

유효한보기입니다. 레지스터와는 별도로 호출 할 때 작동합니다. 내 문제는 (디버깅을 통해) 일단 등록이되면 사용자 페이지로 리디렉션되지만 @login_required 데코레이터가 다시 로그인으로 리디렉션된다는 것입니다. login_decorator를 제거하면 사후 등록 리디렉션이 사용자 페이지에 남겨 지므로 등록시 로그인이 올바르게 발생하지 않아야합니다. – KindOfGuy

0

암호 란은 password1 또는 password입니까?

def register(request): 
    if request.method == 'POST': 
     form = UserCreationForm(request.POST) 
     if form.is_valid(): 
      new_user = form.save() 
      new_user = authenticate(username = request.POST['username'], 
            password = request.POST['password1']) # here? 

      login(request, new_user) 
      # rest of the code... 
관련 문제