开发者

Is it possible to write code for login functionality without using django's authenticate & login method?

开发者 https://www.devze.com 2023-04-12 12:19 出处:网络
Till now I have used django\'s default authenticate and login method for login code for my project.See the code below,

Till now I have used django's default authenticate and login method for login code for my project.See the code below,

"my login code"

def login(request):
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        if(len(username.strip(' ')) == 0):
            return render_to_response('login.html', {'error_message': 'Username should not be empty', 'username': username}, context_instance=RequestContext(request))
        if(len(password.strip(' ')) == 0):
            return render_to_response('login.html', {'error_message': 'Password should not be empty', 'username': username}, context_instance=RequestContext(request))
        authuser = authenticate(username=username, password=password)
        if authuser and authuser.is_active:
            try:
                login(request, authuser)
            except (RuntimeError, TypeError, NameError):
                errmessage = ERROR_MESSAGE % (RuntimeError, TypeError, NameError)
                CapturLog().logdata(request, 'LoginError', 'Login', errmessage)
            else:
                CapturLog().logdata(request, 'Login', 'Login', 'Successfully Logged In')
            return HttpResponseRedirect(reverse('index:home'))
        else:
            retur开发者_如何学Pythonn render_to_response('login.html', {'error_message': 'Invalid User', 'username': username}, context_instance = RequestContext(request))
    return render_to_response('login.html', context_instance=RequestContext(request)) 

This code works well, but I dont want to use django's default method.Instead of that,i wanna use my own method to check for authentication and for login.is it possible or not? I have add some lines into above codes for authentication as below,

 authuser = _authenticate_check(username, password)

 def _authenticate_check(username=None, password=None):
    try:
        if '@' in username:
            user = User.objects.get(email=username)
        else:
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        return None
    # now check the password
    if user.check_password(password):
                return user

Then if I tries for login(request, user), it does not work. It shows an error in this line (ie no backend found).

Is it possible to write a manual code for authentication & login rather than using django's default method?

Thanks in advance !


Simple answer: of course you can write your own. This module is just that a python module that is built for you to use without a lot of work.

Real answer: they do probably everything that you want and more already and the whole philosophy of Django is DRY (Don't Repeat Yourself), so if it is built-in and can handle your situation use it instead of re-inventing the wheel.

You can certainly write your own method for authentication, but you haven't presented a good argument for it in this post. Just because you have poured through the docs and you don't feel comfortable conforming isn't enough. See if the current tools available will work. If not, then it's time to define why/why not and what you need to accomplish what you're going after.

The built-in session, users, groups, are fantastic if you take the time to learn how they work together.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号