Android-Django Image upload

后端 未结 2 1819
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 02:26

I am trying to upload image with android as frontend and django as backend.

The model:

    class Photo(models.Model):
        title = mo         


        
2条回答
  •  走了就别回头了
    2021-02-08 03:09

    Even after you overcome these difficulties, you'll run afoul of Django's requirement that you supply a csrfmiddlewaretoken with your POST parameters. And I don't see how you can obtain that on an Android device; by design that token is there to prevent calling Django backend code (i.e. a "view") from anything other than a Django frontend (i.e. a "template"). I.e. it's designed to thwart doing exactly what you're trying to do.

    You could disable the csrf feature on a particular view -- use the "@csrf_exempt" decorator. And then you can decide if you care enough about security, to work out a substitute of your own for what the csrf thing gives you.

    Or, instead of uploading a picture from an Android app, write a web app to upload a picture, and have your Django project serve up that web app. Your Android app could launch the browser (as an Intent) and point it at that web app. Here's some code that'll do that: https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html (It won't win any beauty contests, but it does work.)

提交回复
热议问题