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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With