现有Django项目中的语法错误

时间:2017-11-09 10:45:17

标签: python django git

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

这就是我所做的

1.克隆回购,

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

想法?

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

1。检查您拥有的python和django版本应与安装的要求兼容。 如果您的python版本高于3. +,请运行服务器

python3 manage.py runserver

否则为python 2.7+版

python manage.py runserver