我正在尝试为我的Django网站创建一个登录页面。起初我尝试了Django附带的所有内置登录功能,但是遇到了问题。我的问题是,目前登录页面一直在尝试重定向到主页。 (我还尝试了另一个页面,没有用)一旦用户登录,重定向页面将呈现为html,但是当它应该是/ mobile /或/并且因为它时,url保持为/ accounts / login /这些链接都不起作用,并且没有加载静态文件。
以下是我的登录视图代码:
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!我已经将问题缩小到我在登录页面的Header中使用的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,因此有一个平淡的登录页面:(
答案 0 :(得分:1)
我认为这个问题与jQuery mobile有关。如jQuery Mobile's form sample所述,您可以通过向表单元素添加data-ajax="false"
来保留jQuery mobile的其他功能,同时修复它。您可能需要在jQuery Mobile上阅读一些内容以使AJAX登录正常工作,如果这就是您所需要的。
答案 1 :(得分:0)
您是否尝试将django.contrib.auth.views.login
与next_page
参数一起使用?我在我的网站上使用过它,效果很好。请仔细阅读this。