我正在使用Django中的多数据库设置。一个用于业务(--database = default),另一个用于存储用户信息(--database = users)。以下是将使用“用户”数据库的应用程序视图:
def auth_and_login(request):
global user
post = request.POST
if post['email'] is u'' or post['password'] is u'':
s = 'Please check login input forms'
return render_to_response('insecure.html', {'s': s})
else:
user = authenticate(username=post['email'], password=post['password'])
if user is not None:
login(request, user)
s = 'Logged in ' + user.first_name + ' ' + user.last_name
return render_to_response('loggedin.html', {'s': s})
else:
s = 'Login fails'
return render_to_response('insecure.html', {'s': s})
def create_user(username, email, password):
global user
user = User(username=username, email=email).using('users')
user.set_password(password)
user.save()
return user
def user_exists(username):
user_count = User.objects.using('users').filter(username=username).count()
if user_count == 0:
return False
return True
def sign_up_in(request):
global user
post = request.POST
if post['email'] is u'' or post['password'] is u'':
s = 'Please check sign-up input forms'
return render_to_response('upsecure.html', {'s': s})
else:
if not user_exists(post['email']):
user = create_user(username=post['email'], email=post['email'], password=post['password'])
user.first_name=post['first']
user.last_name=post['last']
user.save()
s = 'Sign Up Successful ' + user.first_name + ' ' + user.last_name
else:
s = 'Existing User'
return render_to_response('upsecure.html', {'s': s})
当用户注册时,信息存储在正确的数据库中。但是,无法使用信息登录,因为身份验证是针对其他数据库的。如何将身份验证路由到正确的数据库?
答案 0 :(得分:0)
如果您为auth调用设置了auth路由器,则可以为auth路由器中的其他注册模型指定该数据库
if model._meta.app_label == 'signup/whatever':
return 'name_of_auth_db'