我想让用户在第一次登录后每次登录。 步骤进行:
现在下次用户已经注册,我不想再问他用户名和密码了,因为他已经注册并登录了。所以当他点击登录按钮的时候他应该直接无需输入用户名和密码即可重定向到应用程序。
我的登录views.py是
from datetime import datetime
from django.contrib import auth
from django.core import serializers
from django.core.exceptions import ValidationError
@csrf_exempt
def login_android(request):
print "i am in view"
if request.method == "POST":
print "you are in method"
username = request.POST['name']
password = request.POST['password']
#user = auth.authenticate(username=username,password=password)
print username
login_api(request,username,password)
user = auth.authenticate(username=username,password=password)
print user
if user==None:
print "user is not available"
dict = {'username': 'Wrong username or password'}
response = json.dumps(dict)
return HttpResponse(response, mimetype="application/json")
else:
response = json.dumps((model_to_dict(user)), cls=DjangoJSONEncoder)
return HttpResponse(response, mimetype="application/json")
我的登录api.py文件是
def login_api(request,username,password):
print "you are in login api"
user = auth.authenticate(username=username,password=password)
print user
if user:
if user.is_authenticated():
print "user aunticated"
auth.login(request,user)
else:
raise ValidationError
return user
我的登录.html页面
$(function()
{
localStorage['domain'] = "http://122.171.89.190";
var domain = localStorage['domain'];
$('#fac1').on('click', function () {
var username = $("#username").val();
var password = $("#pwd").val();
data = {
name: username,
password: password
};
$.ajax({
url: domain + "/login/login_android_here/",
type: "POST",
data: data,
success: function (response) {
dat=response.username;
if (dat==username){
window.location = 'file:///android_asset/www/posts.html';
}
else{
$("#danger").html(dat);
$("#danger").css("color","red");
}
},
error: function () {
}
});
return false;
});
});
我希望用户一旦点击登录按钮就可以直接重定向到应用程序,如果他已经注册并进入。
答案 0 :(得分:1)
用户点击登录按钮时会调用哪个视图?用户单击登录按钮时是否调用login_android视图?如果是,请在视图的开头添加以下行:
from django.conf import settings
def login_android(request):
if request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
#if the redirect URL is not part of the Django app and you do not know where auth.login(request, user) redirects to, then try
#user = request.user
#auth.login(request, user)
检查用户是否已登录,如果是,则将其重定向到
auth.login(request, user)
重定向到。只需更改
即可return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
行重定向到应用程序的网址(我不确定您将应用程序的URL设置为什么)。
答案 1 :(得分:0)
您可以在注册时执行一项操作,将用户重定向到主应用程序页面。 因此用户将自动登录。 试试这个
if request.method == "POST":
print "you are in method"
username = request.POST['name']
password = request.POST['password']
login_api(request,username,password)
user = auth.authenticate(username=username,password=password)
if user==None:
dict = {'username': 'Wrong username or password'}
response = json.dumps(dict)
return HttpResponse(response, mimetype="application/json")
else:
response = json.dumps((model_to_dict(user)), cls=DjangoJSONEncoder)
return HttpResponse(response, mimetype="application/json")
并在ajax请求中进行必要的更改。