django.core.exceptions.ValidationError:[“ _auth_user_id'值必须是整数。”] Django错误

时间:2019-10-10 19:40:05

标签: python django

关于标题,我在Django中遇到了验证问题。 当我注销时出现此错误,似乎系统正在寻找用户ID,但是,因为我清除了注销的会话(无论如何都无法确定,我将django auth用于登录/注销系统),它在会话中找不到具有相同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

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

我尝试了几件事,但仍然无法弄清正在发生的事情以及在哪里可以找到问题所在。

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方法!

问题出在存储会话变量并检查会话密钥。

如果我没有弄错,则会话密钥“ voucher_id”存储在auth_user_id会话中,这是由于该会话正在寻找存储密钥的user_id所致。

我将在此处发布为解决此问题而对代码进行的更改, 在我的凭证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

对于我使用的注销(关于ozgur的答案,发布在这里Runtime error when trying to logout django):

from django.contrib.auth import logout as django_logout


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

希望这种逻辑对遇到此问题的其他人可能有用,我在django session文档中进行了深入而深入的阅读,以找出答案。

谢谢拉尔夫(Ralf)花时间尝试找出问题所在,并在正确的方向上发给我,寻找会议中的问题。