'列表'对象没有属性' startswith' Django的

时间:2015-10-18 14:58:47

标签: python django

我有一个由FileUpload和另一个字段组成的表单。当我尝试提交该表单时,这个错误加速了。

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/main/2/submit/

Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'main')
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')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/anurag/ebyte/main/views.py" in submit
  47.         f.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
  710.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  738.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
  822.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _do_insert
  861.                                using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _insert
  920.         return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  973.             for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in as_sql
  931.                 for obj in self.query.objs
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save
  315.             file.save(file.name, file, save=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save
  90.         name = self.field.generate_filename(self.instance, name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in generate_filename
  329.         return os.path.join(self.get_directory_name(), self.get_filename(filename))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in get_filename
  326.         return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in inner
  226.             self._setup()
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in _setup
  334.         self._wrapped = get_storage_class()()
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in __init__
  186.         self.location = abspathu(self.base_location)
File "/usr/local/lib/python2.7/dist-packages/django/utils/_os.py" in abspathu
  37.         if not isabs(path):
File "/usr/lib/python2.7/posixpath.py" in isabs
  61.     return s.startswith('/')

Exception Type: AttributeError at /main/2/submit/
Exception Value: 'list' object has no attribute 'startswith'

views.py

@login_required
def submit(request, question_id):
    form = SolutionForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        f = form.save(commit=False)
        f.user_id = request.user
        f.question_id = get_object_or_404(Question, 
                                          pk=question_id)

        # Checks if the user is re-submitting a solved question.
        rows = Solution.objects.filter(user_id=request.user,
                                       question_id=question_id)
        for row in rows:
            if row.solved:
                messages.success(request, "You have already solved this question.")
                return redirect('index')

        #if f.language == cpp:
        #    complie_cpp_task()
        #else:
        #    compile_java_task()


        f.save()
        return redirect('index')
    return render(request, 'main/form.html', {'form': form})

1 个答案:

答案 0 :(得分:0)

因为,您正在使用f = form.save(commit=False),然后添加详细信息。

views.py的第47行,而不是使用f.save()

使用:

f.save_m2m()

请参阅docs以获取说明。