0
views.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))
urls.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,這是不希望的。重定向到從請求發送相同的URL
感謝喬,但沒有任何簡單的解決方案,如果我直接使用request.META.get('HTTP_REFERER')重定向 – Abhimanyu 2012-08-16 14:56:44
當提交表單時HTTP_REFERER將會是登錄表單本身。 HTTP_REFERER是用戶在加載當前頁面時來自的URL,因此如果您想在提交表單後使用它來重定向它們,則必須在最初查看錶單時抓取它。 – 2012-08-16 14:58:44
是啊是的,我已經得到了你的觀點,但我在同一頁面上使用登錄表單和註冊表單,這些是對話框形式,所以它是一種動態的,所以HTTP_REFERER是我希望它重定向的相同url,所以它工作在我的情況,但你的解決方案是更廣泛的,謝謝 – Abhimanyu 2012-08-16 15:05:43