Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\users\holistic\envs\vitadmin\lib\site-packages\django\http\multipartparser.py in read, line 337
Python Executable: C:\Apache24\bin\httpd.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:\users\holistic\envs\vitadmin\lib\site-packages\django\core\handlers\base.py" in get_response
  125.                     response = middleware_method(request, callback, callback_args, callback_kwargs)
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\middleware\csrf.py" in process_view
  174.                     request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\core\handlers\wsgi.py" in _get_post
  137.             self._load_post_and_files()
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\request.py" in _load_post_and_files
  260.                 self._post, self._files = self.parse_file_upload(self.META, data)
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\request.py" in parse_file_upload
  225.         return parser.parse()
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\multipartparser.py" in parse
  149.             for item_type, meta_data, field_stream in Parser(stream, self._boundary):
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\multipartparser.py" in __iter__
  628.             yield parse_boundary_stream(sub_stream, 1024)
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\multipartparser.py" in parse_boundary_stream
  567.     chunk = stream.read(max_header_size)
File "c:\users\holistic\envs\vitadmin\lib\site-packages\django\http\multipartparser.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.

like image 711
xpanta Avatar asked Jan 26 '23 07:01

xpanta


1 Answers

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.

like image 115
user2357112 supports Monica Avatar answered Jan 31 '23 10:01

user2357112 supports Monica