user1163236
user1163236

Reputation: 223

Android-Django user authentication

I am trying to access the current logged in user from the android app in django. but the android application stops unexpectedly giving me fatal exception. the android side code accepts json response when i comment the line

at django side i have this code ,

@login_required 
def add_healthrecord(request):
    if request.method=='POST':
        if not request.user.is_authenticated(): # if user is not logged in
            response_data=[{"success": "0"}]
            return HttpResponse(simplejson.dumps(response_data),mimetype='application/json')
        current_user=request.User
        response_data=[{"success": "1"}]
        return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')

can please somebody help me here. actually i am trying to use user as foreign key thats why i need to access it. the above django view give me response when i comment all the user related lines

Upvotes: 1

Views: 747

Answers (1)

g19fanatic
g19fanatic

Reputation: 10941

Without any more information, it seems like you're not passing the csrf token in with your post command. When using the @login_required decorator, you need to pass the csrf token (essentially the users session id) in with the request or you will not be able to post data to that view. Checkout the Django Documentation for some more info.

Upvotes: 1

Related Questions