现有 Django 项目中的语法错误

Posted

技术标签:

【中文标题】现有 Django 项目中的语法错误【英文标题】:Syntax error in existing Django project 【发布时间】:2018-04-22 07:51:15 【问题描述】:

我第一次安装了现有的 Django 项目,但在启动服务器时遇到了问题python manage.py runserver

这就是我所做的

1.克隆repo,

2.制作虚拟环境

3.Pip install requirements.txt

4.生成访问令牌和密钥并放入secrets.sh。我在settings.pysecrets.sh 中有相同的 SECRET_KEY,并且我已将 secrets.sh 添加到.gitignore

5.更改settings.py如下:

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'USER': 'name',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    

我无法运行python manage.py migrate 结果如下:

(tag_gen) local_user@local_user:~/Repo/tag_gen/generator$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x7febe4712488>
Traceback (most recent call last):
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/local_user/Repo/tag_gen/generator/generator/urls.py", line 23, in <module>
    url(r'^etg/', include('generatorApp.urls', namespace='generatorApp')),
  File "/home/local_user/Repo/tag_gen/tag_gen/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/local_user/Repo/tag_gen/generator/generatorApp/urls.py", line 3, in <module>
    from . import views
  File "/home/local_user/Repo/tag_gen/generator/generatorApp/views.py", line 170
    def view_index(request: WSGIRequest):
                          ^
SyntaxError: invalid syntax

想法?

【问题讨论】:

使用 sqlite3 时,您可以废弃 DATABASES['default']ENGINENAME 中的所有字段。 【参考方案1】:

1.检查你拥有的python和django版本应该与安装的要求兼容。 如果你的 python 版本超过 3.+ 运行服务器

python3 manage.py runserver

如果是 python 版本 2.7+,则为 else

python manage.py runserver

【讨论】:

【参考方案2】:

您尝试运行的项目使用 Python ≥ 3.5,但您尝试在 2.7 中运行它。

语法(request: WSGIRequest): 是一个类型提示。它是由a few years ago 引入的,但仅添加到较新版本的 Python 3 中。没有努力支持 Python ≤ 3.4。

您需要查找有关如何使用足够高版本的 Python 创建virtualenv 的说明。此更改基于操作系统,因此详细说明可能超出此问题的范围,but there is plenty of advice on the topic already。

【讨论】:

是的,你是对的。我准备了新的 venv 和作品。谢谢。

以上是关于现有 Django 项目中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章

django中的语法错误

Google App Engine 中的 Django 模板语法错误

升级到Django 2.1的urls.py路径中的语法错误

DJango 中 URLS.py 中的无效语法错误

缺少变量值时 Django 模板中的 Javascript 语法错误

Django - 在 javascript 函数中使用 和 % % 标签时出现语法错误