5

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.