I am developing this android app which uploads images to my website which is developed on django.
I have used django-rest-framework
to create this api for uploading files. Now I need to use Forms to save the images(see the code below)
@api_view(['GET', 'POST'])
def create_image(request):
ImageModel = get_image_model()
ImageForm = get_image_form(ImageModel)
if request.user.is_authenticated():
if request.method == 'POST':
image = ImageModel(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
image.file_size = file.size
form.save()
But it gives the error CSRF verification failed. Request aborted.
I've tried @csrf_exempt
but still the same error.
As I re-read the documentation of Django-rest-framework, I found THIS which says:
Note that if deploying to Apache using mod_wsgi, the authorization header is not passed through to a WSGI application by default, as it is assumed that authentication will be handled by Apache, rather than at an application level.
If you are deploying to Apache, and using any non-session based authentication, you will need to explicitly configure mod_wsgi to pass the required headers through to the application.
So, the authentication is being handled by Apache and not at the application level.