0

我无法弄清楚为什么身份验证在此注册视图中不起作用。我这么说是因为用户正在输入数据库并且它正在重定向到下一个所需的页面(它有一个 login_required 装饰器),但随后它又重定向回登录页面。Authenticate 使用给定的值在 shell 中返回新用户。我也在这里调试并粘贴了:http ://dpaste.org/J1HZ5/出了什么问题?

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

网址.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)),
)

谢谢

4

4 回答 4

0

您可能想发布您的 UserCreationForm 代码,但我注意到的一件事是您正在运行authenticate(...)调用,但实际上并没有检查它的结果然后重定向。查看 Django 源代码,有一个内置的身份验证表单django.contrib.forms.AuthenticationFormauthenticate()

于 2012-09-13T21:46:33.063 回答
0

您的问题出在这一行:

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

确保user是一个有效的视图。

于 2012-09-14T05:26:01.380 回答
0

您是否安装了 django 会话中间件?

于 2012-09-13T21:45:20.380 回答
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...
于 2017-07-17T19:23:54.600 回答