as for the title I have this problem with validation in Django. This error occours when i Logout, it seems like the system is looking for the user id but, because i clear the session with the log out(not sure anyway if this is right, I use django auth for the login/logout system), it can't find any user with same id in the session and is giving me this error. So I tried removing all the user call i have in the code but it is still not working at all.
Here as follow the full error log.
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
I can login if i remove the "{% if request.user.is_authenticated %}" from my template. Then i have to insert it again to show the menu for the logged user and everything work perfect until i do again the logout and it run again in the same error.
I tried several things and still i can't sort out what's happening and where to go to search for what is the problem.
I'm new in django and are few days I'm trying to solve this problem, if anyone can give me the right direction to solve this problem it would be much appreciate.
I found the solution!
The problem was in storing the session variable and checking for the session key.
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.
I'll post here the changes i have made to my code to solve this, In my voucher context.py (where i am checking if the voucher code is in session):
Before:
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
Fixed code:
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
For the logout I used (as for the answer of ozgur posted here 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'))
Hope this logic could be useful to someone else that run in this problem, I had a long and deep reading at the django session documentation to sort it out.
Thank you Ralf to spent time trying to figure it out where could have been the problem and to sending me in the right direction looking for problem in the session.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.