简体   繁体   English

Python django 代码中的“SyntaxError:无效语法”

[英]Python “SyntaxError: invalid syntax” in django code

I am trying to create an app that will enable a user to upload a file.我正在尝试创建一个使用户能够上传文件的应用程序。 I want to create a model that will handle the file upload.我想创建一个 model 来处理文件上传。

from django import forms

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file  = forms.FileField()

But upon doing this when I syncdb I get the following error.但是当我同步数据库时这样做时,我收到以下错误。

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/validation.py", line 36, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/roy/projects/qsourcing/qsl/models.py", line 5
    title = forms.CharField(max_length=50)                                        file  = forms.FileField()   
                                                                                     ^
SyntaxError: invalid syntax

Start reading your backtrace from the bottom:从底部开始阅读您的回溯:

  File "/home/roy/projects/qsourcing/qsl/models.py", line 5
    title = forms.CharField(max_length=50)                                        file  = forms.FileField()   
                                                                                     ^
SyntaxError: invalid syntax

In this case there is a problem in line 5 of models.py .在这种情况下, models.py第 5 行存在问题。 You are missing a new line你错过了一个新行

Soner is correct.索纳是正确的。 This issues is most likely caused by incorrect formatting.此问题很可能是由不正确的格式引起的。 I have come across this error a few times after I copied code from a tutorial or blog and then pasted it directly into my project.在我从教程或博客复制代码然后将其直接粘贴到我的项目中后,我曾多次遇到此错误。 If you see this error after copy and pasting code from a tutorial or blog, simply delete the pasted code and manually type out the code instead.如果您在从教程或博客复制和粘贴代码后看到此错误,只需删除粘贴的代码并手动输入代码即可。

Best,最好的,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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