Skip to content
Advertisement

Django stops with “generator raised StopIteration” when html form allows for file upload

My setup is Windows 10, Python 3.7, Apache 2.4/mod_wsgi. When I add this enctype="multipart/form-data" in my form (just by adding this attribute, only — no files are attached to the form) I get this error when submitting:

Django Version: 1.8.5
Exception Type: RuntimeError
Exception Value: generator raised StopIteration
Exception Location: c:usersholisticenvsvitadminlibsite-packagesdjangohttpmultipartparser.py in read, line 337
Python Executable: C:Apache24binhttpd.exe
Python Version: 3.7.3

My Django code is this:

elif request.method == "POST":
    rid = request.POST.get("recipe", "")
    title = request.POST.get("title")
    content = request.POST.get("content")
    tag_names = request.POST.getlist("tags")
    image = request.FILES.get("image")
    if rid:
        recipe = get_object_or_404(FoodRecipe, pk=rid)
    else:
        recipe = FoodRecipe.objects.create(title=title)
    recipe.content = content
    recipe.title = title
    if image:
        recipe.featured = image
    for tn in tag_names:
        tag, cr = Tag.objects.get_or_create(
            name=tn
        )
        recipe.tags.add(tag)
    recipe.save()

And this is full traceback:

Environment:
Request Method: POST
Request URL: http://192.168.1.250/recipes/add/
Django Version: 1.8.5
Python Version: 3.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tinymce',
 'sekizai',
 'sorl.thumbnail',
 'recipes',
 'guides',
 'inbox',
 'appdata',
 'account',
 'customer',
 'core')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'account.middleware.PasswordChangeMiddleware.PasswordChangeMiddleware')


Traceback:
File "c:usersholisticenvsvitadminlibsite-packagesdjangocorehandlersbase.py" in get_response
  125.                     response = middleware_method(request, callback, callback_args, callback_kwargs)
File "c:usersholisticenvsvitadminlibsite-packagesdjangomiddlewarecsrf.py" in process_view
  174.                     request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
File "c:usersholisticenvsvitadminlibsite-packagesdjangocorehandlerswsgi.py" in _get_post
  137.             self._load_post_and_files()
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttprequest.py" in _load_post_and_files
  260.                 self._post, self._files = self.parse_file_upload(self.META, data)
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttprequest.py" in parse_file_upload
  225.         return parser.parse()
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttpmultipartparser.py" in parse
  149.             for item_type, meta_data, field_stream in Parser(stream, self._boundary):
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttpmultipartparser.py" in __iter__
  628.             yield parse_boundary_stream(sub_stream, 1024)
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttpmultipartparser.py" in parse_boundary_stream
  567.     chunk = stream.read(max_header_size)
File "c:usersholisticenvsvitadminlibsite-packagesdjangohttpmultipartparser.py" in read
  337.         out = b''.join(parts())

Exception Type: RuntimeError at /recipes/add/
Exception Value: generator raised StopIteration

Any ideas what is going wrong?

PS: Same django application worked fine in Linux/Nginx/Gunicorn/Python2.7 setup. So, I guess it must some misconfiguration between Django/Python/Apache.

Advertisement

Answer

Your Django is very old and you need to update. This is a Python 3.7 compatibility problem that the Django devs already fixed four years ago, back when it was just a PendingDeprecationWarning on Python 3.5.

In fact, you are on the very last Django version that doesn’t have the fix. Even 1.8.6 has the fix.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement