как читать и обрабатывать c файлов в Django для подсчета слов внутри файла - PullRequest
0 голосов
/ 03 мая 2020

Я пытаюсь позволить пользователю добавить файл .do c, используя форму в Django, но он продолжает выдавать ошибку:

a bytes-like object is required, not 'str'

Вот мой код:

def upload_quota(request):
    upload_file_form = FileReplaceForm(request.POST , request.FILES)
    if request.method == 'POST':
        if upload_file_form.is_valid():
            file = upload_file_form.cleaned_data['file']
            data = file.read()
            word = data.split(" ")
            print(len(word))

Вот полный traceback :

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/upload/quota/

Django Version: 3.0.5
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Apex__System.apps.ApexSystemConfig',
 'django_filters',
 'storages',
 'webpush']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware']



Traceback (most recent call last):
  File "/home/ahmed/Documents/projects/Apex_system/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/ahmed/Documents/projects/Apex_system/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/ahmed/Documents/projects/Apex_system/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ahmed/Documents/projects/Apex_system/venv/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/ahmed/Documents/projects/Apex_system/Apex__System/views.py", line 472, in upload_quota
    word = data.split(" ")

Exception Type: TypeError at /upload/quota/
Exception Value: a bytes-like object is required, not 'str'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...