Android-Django user authentication

空扰寡人 提交于 2019-12-11 04:42:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/9851972/android-django-user-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!