Django的认证用户搜索工作不正常
问题描述:
我所试图做的是以下几点:Django的认证用户搜索工作不正常
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
from django.contrib.auth.models import User
而且,为什么DoesNotExist错误发生(当一个正在寻找在认证系统中的用户)目前尚不清楚。为什么我得到这个错误?
TIA
这是怎样的 “应用程序” 被构造
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')
答
你试图让用户不存在。试着找出你的确切的username
变量,并用admin中的数据库检查它。你会看到它不存在。例如,你可以做以下捕获错误:
try:
user = User.objects.get(username=username)
myprofile = user.userprofileinfo
num_results = Mstrauthownerrdx.objects.filter(logonid=username).count()
except User.DoesNotExist:
num_results = 0
答
你发送一个GET请求。在您的代码中,您尝试从username
POST参数获取用户名。由于视图没有用户名您的观点是试图获取没有username
这恐怕在你的数据库中不存在的User
对象。
要修复它,您必须将username
作为GET参数传递,或发送带有username
作为参数的POST请求。
感谢您的答复。我看到我没有获得价值回归的原因是因为这部分代码:“username = request.POST.get('username')”返回None。 –