Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code 200 httpresponse on django

I´m doing a service basic login, and I need to answer with code 200 and JSON in a Django view, but I don't know if is this the correct form using HttpResponse library?

def Login(email,password):      
    User=CUser()        
    if User.is_valid(email,password) :      
        user=User.find(email)
        datos['Id'] = str(user['Id'])
        datos['Name'] = user['Name']
        datos['LastName'] = user ['LastName']           
        datos['Email'] = user ['Email']
        return HttpResponse(json.dumps(data), content_type = "application/json",status_code = 200)
    else:
        return HttpResponse( content_type = "application/json",status_code = 400)

I will use this response in android login, , and for that i need the status codes like django return on console

like image 306
Alexander José Huamán Mejia Avatar asked Sep 12 '14 19:09

Alexander José Huamán Mejia


1 Answers

Yup, HttpResponse.status_code can be set like this.

Note that you can improve your code by utilizing, introduced in Django 1.7, JsonResponse:

An HttpResponse subclass that helps to create a JSON-encoded response. It inherits most behavior from its superclass with a couple differences:

Its default Content-Type header is set to application/json.

like image 160
alecxe Avatar answered Sep 28 '22 22:09

alecxe