我想爲我的Django網站創建一個登錄頁面。起初我嘗試了Django附帶的所有內置登錄函數,但是我遇到了一個問題。我的問題是,目前登錄頁面一直在嘗試重定向到主頁。 (我也嘗試了另一個頁面,沒有工作)一旦用戶登錄,重定向頁面呈現爲一個html,但是當它應該是/ mobile/or /時,url保持爲/ accounts/login /這些鏈接都不起作用,並且靜態文件沒有加載。Django登錄呈現重定向模板,但留在登錄URL
下面是我的登錄視圖代碼:
def login(request):
if request.method == "POST":
redirect_to = request.POST['next']
print 'method is post'
form = AuthenticationForm(data=request.POST)
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if form.is_valid() and user is not None and user.is_active:
print 'form is valid'
netloc = urlparse.urlparse(redirect_to)[1]
print 'original redirect_to is: ',redirect_to
# Use default setting if redirect_to is empty
if not redirect_to:
print 'changing redict to default: ', redirect_to
redirect_to = settings.LOGIN_REDIRECT_URL
print 'redirect to is: ',redirect_to
# Heavier security check -- don't allow redirection to a different
# host.
#elif netloc and netloc != request.get_host():
# redirect_to = settings.LOGIN_REDIRECT_URL
# Okay, security checks complete. Log the user in.
print form.get_user()
auth.login(request, user)
print 'user logged in'
return redirect(reverse('builds.views.mobile_view'))
else:
redirect_to = request.REQUEST.get('next')
form = AuthenticationForm(request)
c = {
'next':redirect_to,
'form': form,
}
print 'returning render to response login'
return TemplateResponse(request,'registration/login.html', context = c)
由於內置在登錄視圖似乎並不奏效,我想如果我寫我自己也可以工作,但是問題依然存在。
這裏是我的登陸的html代碼:
<!-- Start of first page: START -->
<div data-role="page" class="type-interior" id="LOGIN" data-theme="a">
{% if form.errors %}
<div data-role="header" data-theme="a" data-content-theme="a">
<h1>Your username and password didn't match. Please try again.</h1>
</div><!-- /header -->
{% else %}
<div data-role="header" data-theme="a" data-content-theme="a">
<h1>Please Enter Your Username and Password</h1>
</div><!-- /header -->
{% endif %}
<form method="post" action="/accounts/login/" id="login_form">
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="Login" id="button"/>
<input type="hidden" name="next" value="{{ next|escape }}" />
</form>
<div data-role="footer" data-theme="a">
<h4></h4>
</div>
</div>
請幫幫我!我覺得我已經嘗試了一切。也請告訴我是否應該包含任何其他代碼。
編輯:
OKAY!我已經將問題縮小到了我在登錄頁面的頁眉中使用的java腳本內容(我正在使用jquery)。標題是:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Install Builds</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1 maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="{{ STATIC_URL }}blablahblachname.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile.structure-1.1.0.min.css" />
<!--<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>-->
<link rel="apple-touch-icon-precomposed" href="{{ STATIC_URL }}static/homepage_icon.png" />
<link rel="icon" href="{{ STATIC_URL }}homepage_icon.png" type="image/vnd.microsoft.icon" />
</head>
是否有解決此問題的方法?或者我必須完全排除JavaScript,因此有一個看起來平淡的登錄頁面:(
謝謝!這工作得很好。 – karkle 2012-07-19 18:30:10