无法从oracle数据库验证用户(python django)

时间:2016-11-18 06:36:59

标签: python django oracle

我是python django的新手。我试图创建一个用户登录的简单应用程序并将其连接到Oracle 11g数据库。我的联系很成功。我的问题是当我使用用户数据(用户ID和密码)时。它总是将我重定向到无效页面。它还指向auth行说(预期:)错误。

这是我的代码

views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.contrib import auth
from django.shortcuts import get_object_or_404, render
from .models import OnlineUser

    def login(request):
    return render(request, 'login.html')

def auth_view(request):
    userid = request.POST.get("userid", '')
    userpass = request.POST.get("userpass", '')
    user = auth.authenticate(OnlineUser.USERID=userid, OnlineUser.USERPASS=userpass)
    #user = auth.authenticate(userid=userid, userpass=userpass)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/samplesite/loggedin')
    else: 
        return HttpResponseRedirect('/samplesite/invalid')

def loggedin(request):
    user = get_object_or_404(OnlineUser, pk=id)
    return render(request, 'login.html', {'user': user})


def invalid(request):
    return render(request, 'invalid.html')

def logout(request):
    auth.logout(request)
    return render(request, 'logout.html')

models.py

from __future__ import unicode_literals

from django.db import models

class OnlineUser(models.Model):
    ID = models.IntegerField(primary_key=True)
    USERID = models.TextField()
    USERPASS = models.TextField()

    class Meta:
        managed = False
        db_table= 'TBL_ONLINE_USER'

    def __str__(self):
        return str(self.ID)

模板/ login.html的

<div>
    <form action="/samplesite/auth_view/" method="POST">{% csrf_token %}
        <input type='text' size='25' name="userid" id="userid" class="form-control" placeholder="User ID"/>
        <br/>
        <input type='password' size='25' name="userpass" id="userpass" class="form-control"  placeholder="Password"/>
        <br/>
        <input type="submit" value="login"/>
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

it should be

userid = request.POST.get("userid", '')
userpass = request.POST.get("userpass", '')
user = authenticate(username=userid, password=userpass)

我希望userid是用户的用户名