Skip to main content
 首页 » 编程设计

python之现有 Django 项目中的语法错误

2025年05月04日42txw1958

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

的问题

这是我做的

1.克隆仓库,

2.制作虚拟环境

3.pip安装要求.txt

4.生成访问 token 和 key 并放入secrets.sh。我在 settings.py 和 secrets.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 

想法?

请您参考如下方法:

您尝试运行的项目使用 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 .