我无法弄清楚为什么身份验证在此注册视图中不起作用。我这么说是因为用户正在输入数据库并且它正在重定向到下一个所需的页面(它有一个 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)),
)
谢谢