视图.py
def login_user(request):
if request.POST:
sform = LoginForm(request.POST)
rform = UserForm()
if sform.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username = username,password = password)
if user is not None:
if user.is_active:
login(request,user)
return redirect(request.path)
else:
state = "Your account is not active."
#or first set the user to is active and then log in
return redirect('acitvate_account',user = user)
return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request))
else:
state = "Incorrect Username or Password"
#sform = LoginForm()
form = LoginForm()
return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
else:
state = "Invalid form"
dLoginForm = LoginForm()
rform = PartialSignupForm()
return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
else:
dLoginForm = LoginForm()
rform = PartialSignupForm()
return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request))
网址.py
urlpatterns += patterns('beenthere.views',
#beenthere urls
(r'^$','index'),
(r'^home2/$','category'),
(r'^accounts/login/$','login_user'),
(r'^accounts/register/$','register_user'),
(r'^home/(?P<user>\w*)$','home'),
(r'^logout/','logout_user'),
)
怀疑
我想如何重定向发送请求的相同 url,我尝试使用 request.path 但它将我重定向到 /accounts/login/ url,这是不需要的。