DJango身份验证用户搜索无法正常工作

时间:2017-10-19 12:44:12

标签: django authentication

我要做的是以下内容:

1. Retrieve a User record (from the DJango authentication system) is in
    the DB
2. get the Username (from that record)
3. Use the "username" to look for a record in a *different* table.
4. If the record *is not* there (in the *different* table), then create one.

即使我在views.py中有以下内容,我在查看User表中的内容时出现错误

  

来自django.contrib.auth.models导入用户

此外,还不清楚为什么会出现 DoesNotExist 错误(当人们在身份验证系统中查找用户时)。为什么我收到此错误?

TIA

这是错误消息 enter image description here

这就是“app”的结构

enter image description here

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from authinduction.models import Mstrauthownerrdx
from django.contrib.auth.models import User

def inductowner(request):

    username = request.POST.get('username')
    user = User.objects.get(username=username)

    myprofile = user.userprofileinfo

    num_results = Mstrauthownerrdx.objects.filter(logonid=username).count()

    if not ( num_results == 0  or num_results == 1 ):
        raise ValueError('Number of items found '+ num_results + ' is not valid')

    if num_results == 0:
        u = Mstrauthownerrdx.objects.create(logonid=username, emailaddr=user.email,
                worktype=1, memo='OWNER', active=1, formpagelastfilled=myprofile.lastpgprocno,
                formcomplete=myprofile.nextpgprocno, reclocktype=1, reclockid=1)

        u.save()

    return render(request, 'authinduction/owner/index.html')

2 个答案:

答案 0 :(得分:0)

您尝试获得的用户不存在。尝试找出您确切的username变量所说的内容,并在管理员中使用数据库进行检查。你会发现它并不存在。例如,您可以执行以下操作来捕获错误:

try:
     user = User.objects.get(username=username)
     myprofile = user.userprofileinfo
     num_results = Mstrauthownerrdx.objects.filter(logonid=username).count()
except User.DoesNotExist:
     num_results = 0

答案 1 :(得分:0)

您正在发送GET请求。在您的代码中,您尝试从username POST参数获取用户名。由于视图没有用户名,因此您的视图正在尝试获取User对象而不使用username,我恐怕您的数据库中不存在该对象。

要修复它,您必须将username作为GET参数传递,或者以username作为参数发送POST请求。

相关问题