Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Type error at/ <User: user1> is not JSON serializable

I got a type error showing " is not JSON serializable". After adding session related code this error comes.

views.py

def jlogin(request):
    if request.method == 'POST':
        username = request.POST['uname']
        password = request.POST['pword']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request,user)
            print(user)
            request.session['user']=user
            return redirect('/feeds/')
        else:
            return render(request,'join/login.html')
    else:
        user = request.session.get('user')
        print(user)
        if user is None:
            return render(request,'join/login.html')
        else:
            return render(request,'feeds/feeds.html')

login.html

<!DOCTYPE html>
<html>
<head>
    <title>Login to dEEPsIGMA</title>
</head>
<body>
<form action="." method="post" enctype="multipart/form-data">
    {% csrf_token %}
    Username :
    <input type="text" name="uname"/> <br>
    Password :
    <input type="text" name ="pword"/>
    <input type="submit">

</form>
</body>
</html>

Traceback:

File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\core\handlers\base.py" in get_response
  207.                 response = middleware_method(request, response)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\contrib\sessions\middleware.py" in process_response
  49.                         request.session.save()
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\contrib\sessions\backends\db.py" in save
  58.             session_data=self.encode(self._get_session(no_load=must_create)),
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\contrib\sessions\backends\base.py" in encode
  89.         serialized = self.serializer().dumps(session_dict)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\core\signing.py" in dumps
  89.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "C:\Python27\lib\json\__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "C:\Python27\lib\json\encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "C:\Python27\lib\json\encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /
Exception Value: <User: user1> is not JSON serializable

please help to solve this error. Explain why this error is occurred .

like image 311
Wickkiey Avatar asked Jan 03 '15 16:01

Wickkiey


1 Answers

The line that is generating the error is:

request.session['user']=user

Django has been using JSON to serialize the session data after version 1.6. More about this on Django documentation. User object is not JSON serializable. So, you need to ensure that the object you are storing in session variable should be JSON serializable. Prior to Django 1.6, the serializer used by Django was PickleSerializer for which your code would run perfectly.

To make your code work, you can do one of the following:

  1. Store user's id instead of User object.
  2. Create your own serializer for User object.
like image 144
Sudip Kafle Avatar answered Nov 07 '22 00:11

Sudip Kafle