简体   繁体   中英

Upload file with FileField and nginx just throw errors?

I'm new to Django, I'm learning how to handle uploading file with django, I did the same things with the document, but my nginx just throw out errors,

here is my views.py:

@csrf_exempt
def upload_view(request):
    if request.method == 'POST':
        form = UploadItemForm(request.POST, request.FILES)
        if form.is_valid():
            return HttpResponse('successfully uploaded')
        else:
            return HttpResponse('upload failed')
else:
    if request.user.is_authenticated():
        form = UploadItemForm()
        return render(request, 
            'design/upload.html', 
            {'form': form,
            'username': request.user.username})
    else:
        return HttpResponse("you have to login")

model.py

class Item(models.Model):
    name = models.CharField(max_length = 100)
    description = models.TextField(max_length = 1000)
    uploadfile = models.FileField()

class UploadItemForm(ModelForm):
    class Meta:
        model = Item

my template:

<form enctype="multipart/form-data" method="post" action="/design/">
 {% csrf_token %}
 {{ form.as_p }}

<input type="submit" value="upload" />
</form>

if I upload some text-based file (size is very small), everything is OK, when upload other formats, or large csv file, the code failed on

form = UploadItemForm(request.POST, request.FILES)

nginx says

Sorry, the page you are looking for is currently unavailable. Please try again later.

I use nginx+uwsgi+django, so is it related to the uwsgi and nginx setup?

Ah, I solved it. For some unknown reason. It seems that, when the request size is beyond some threshold, nginx will put data on client_body_temp/ directory, for some reason(I don't know it), this directory in my setup is read-only by root???? change the permission, then everything goes fine

For additional info:

client_max_body_size 4M; //4mb
default_type text/plain; //file format

Here are the list of functions: http://nginx.org/en/docs/http/ngx_http_core_module.html

Read also this forum: http://forum.slicehost.com/index.php?p=/discussion/1714/nginx-413-when-uploading-file-1mb-or-larger/p1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM