Django:Django生成的sql查询导致错误
更新问题: Django向我提供以下sql查询:Django:Django生成的sql查询导致错误,sql,django,postgresql,authentication,postgresql-9.1,Sql,Django,Postgresql,Authentication,Postgresql 9.1,更新问题: Django向我提供以下sql查询: SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "au
SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" = %s args=('andrew',);
如果在postgresql命令行中执行sql查询,则会出现以下错误:
ERROR: syntax error at or near "%"
LINE 1: ..." FROM "auth_user" WHERE "auth_user"."username" = %s args=(...
^
但是,当我稍微修改语句时,就会从postgresql获得cirrect结果
SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" = 'andrew';
Django生成的查询是否不正确?
大家好 非常简单的代码让我抓狂: 我想使用从user_auth提取用户信息
user = get_object_or_404(User, pk = request.user.pk)
但是,我在Django中收到一条错误消息:
'NoneType' object does not support item assignment
当我检查sql查询并在psql命令行中执行它时,psql还会给我一条错误消息,这使我认为该语句可能不正确
psql语句是:
SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = %s ; args=(7,)
为什么在WHERE语句中显示%s?用户id不是字符串。
我相信解决方案必须非常简单,但我能找出问题所在。
谢谢你的帮助和建议
附加说明
我正在使用django_social_auth包进行用户身份验证。一旦第三方清除凭据,用户将被定向到仪表板站点,因此我假设request.user不是None
完全回溯
Environment:
Request Method: GET
Request URL: http://login.test.com:8000/profile/dashboard/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.gis',
'django.contrib.messages',
'django.contrib.markup',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'django.contrib.humanize',
'guardian',
'easy_thumbnails',
'userena',
'userena.contrib.umessages',
'south',
'django_extensions',
'debug_toolbar',
'social_auth',
'djangoratings',
'about',
'apps.profiles',
'apps.map',
'apps.contact',
]
Installed Middleware:
['django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/Users/neurix/Development/test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/neurix/Development/test/test/apps/profiles/views.py" in dashboard
35. extra_context['username'] = user.username
Exception Type: TypeError at /profile/dashboard/
Exception Value: 'NoneType' object does not support item assignment
views.py
...
31: print "user: %s" %(request.user.pk)
32: user = get_object_or_404(User, pk = request.user.pk)
33:
34:
35: extra_context['username'] = user.username
36: if user.first_name:
37: extra_context['name'] = user.first_name
...
可能是该请求。用户是否为无
if request.user is not None:
user = get_object_or_404(User, pk = request.user.pk)
%s
是通用占位符,不表示字符串。尝试发布回溯。您能显示完整的回溯吗?添加了回溯。谢谢你的帮助!现在我们需要查看包含/Users/neurix/Development/test/test/apps/profiles/views.py:35
的代码。嗨,Ignacio,我已经在第35行周围添加了几行。