Django return JSON with Logged in Cookie
<p>I want to design a simple Django "RESTful" API without the need of using django-rest-framework.</p>

<p>The endpoint is <code>/api/login</code> which accept only POST method with <code>username</code> and <code>password</code> in json. I want it to return <code>{"status" : 0}</code> for success with the session cookie to be used later at my other endpoints. Only when the user successfully logged in, the cookie will be set.</p>

<p>I have imported the <code>JsonResponse</code> object from <code>django.http</code> but not very sure how to set the session for the user.</p>

<pre><code>def login(request):
    # some code omitted, like if request.method == "POST"
    credential = json.loads(request.body)
    username = credential['username']
    password = credential['password']
    user = authenticate(username=username, password=password)
    if user:
        if user.is_active:
            auth_login(request, user)
        else:
            return JsonResponse({"status" : 1, "err" : "Your account has been disabled"})
        # TODO: Now return what?
</code></pre>

<p>I have browsed the <a href="https://docs.djangoproject.com/en/1.10/ref/request-response/#jsonresponse-objects" rel="nofollow">document</a> but failed to find any useful tip.</p>

<p>I tested it out, that I can just use <code>return JsonResponse({'status' : 0})</code> because the cookies are registered with Django because of <code>authenticate</code>.</p>

python json django cookies