繁体   English   中英

django.core.exceptions.ValidationError: [“'_auth_user_id' value must be an integer.”] Django 上的错误

[英]django.core.exceptions.ValidationError: [“'_auth_user_id' value must be an integer.”] error on Django

至于标题,我在 Django 中的验证存在这个问题。 当我注销时发生此错误,似乎系统正在寻找用户 ID,但是,因为我在注销时清除了 session(无论如何不确定这是否正确,我使用 django 身份验证用于登录/注销系统),它在 session 中找不到任何具有相同 ID 的用户,并且给了我这个错误。 所以我尝试删除我在代码中的所有用户调用,但它仍然无法正常工作。

以下是完整的错误日志。

Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 941, in to_python
 return int(value)
ValueError: invalid literal for int() with base 10: '_auth_user_id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner       
 response = get_response(request)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response   
 response = self.process_exception_by_middleware(e, request)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response   
 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\gello\Desktop\Projects\buyit\home\views.py", line 12, in index
 return render(request, "index.html", {"products": products, "product_reviews": product_reviews})
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\shortcuts.py", line 36, in render
 content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader.py", line 62, in render_to_string    
 return template.render(context, request)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py", line 61, in render     
 return self.template.render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 171, in render
 return self._render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 163, in _render
 return self.nodelist.render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 937, in render
 bit = node.render_annotated(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 904, in render_annotated     
 return self.render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py", line 150, in render        
 return compiled_parent._render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 163, in _render
 return self.nodelist.render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 937, in render
 bit = node.render_annotated(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 904, in render_annotated     
 return self.render(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py", line 302, in render        
 match = condition.eval(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py", line 876, in eval
 return self.value.resolve(context, ignore_failures=True)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 671, in resolve
 obj = self.var.resolve(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 796, in resolve
 value = self._resolve_lookup(context)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py", line 829, in _resolve_lookup      
 current = current[bit]
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 256, in inner
 self._setup()
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 392, in _setup
 self._wrapped = self._setupfunc()
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\middleware.py", line 24, in <lambda>    
 request.user = SimpleLazyObject(lambda: get_user(request))
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\middleware.py", line 12, in get_user    
 request._cached_user = auth.get_user(request)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\__init__.py", line 182, in get_user     
 user_id = _get_user_session_key(request)
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\__init__.py", line 59, in _get_user_session_key
 return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
File "C:\Users\gello\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 946, in to_python
 params={'value': value},
django.core.exceptions.ValidationError: ["'_auth_user_id' value must be an integer."]
[10/Oct/2019 20:21:38] "GET / HTTP/1.1" 500 184085

如果我从模板中删除“{% if request.user.is_authenticated %}”,我可以登录。 然后我必须再次插入它以显示登录用户的菜单,并且一切正常,直到我再次注销并且它再次以相同的错误运行。

我尝试了几件事,但我仍然无法弄清楚发生了什么以及 go 到哪里搜索问题所在。

我是 django 的新手,几天前我正在尝试解决这个问题,如果有人能给我正确的方向来解决这个问题,我将不胜感激。

我找到了解决方案!

问题在于存储 session 变量并检查 session 密钥。

If i did not get it wrong The session key "voucher_id" was stored in the auth_user_id session and this was causing the problem because of the session that was looking for the user_id where was stored the key.

我将在这里发布我对代码所做的更改以解决这个问题,在我的凭证 context.py 中(我正在检查凭证代码是否在会话中):

前:

if ['voucher_id'] not in request.session:
        voucher_id = request.session.get('voucher_id')
        try:
            code = Voucher.objects.get(id=voucher_id)
        except Voucher.DoesNotExist:
            code = None
    else:
        None

固定代码:

if 'voucher_id':
        voucher_id = request.session.get('voucher_id')
        try:
            code = Voucher.objects.get(id=voucher_id)
        except Voucher.DoesNotExist:
            code = None
    else:
        None

对于我使用的注销(至于尝试注销 django 时在此处发布的 ozgur 的答案运行时错误):

from django.contrib.auth import logout as django_logout


@login_required
def logout(request):
    django_logout(request)
    return  HttpResponseRedirect(reverse('index'))

希望这个逻辑对遇到这个问题的其他人有用,我在django session文档中进行了长时间而深入的阅读以对其进行整理。

感谢拉尔夫花时间试图找出问题所在,并让我朝着正确的方向寻找 session 中的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM