0

这是我在 Stack Overflow 上的第一个问题。我已经学习了大约一个月的 Python 和两周的 Django。我尝试在我的 MacBook Pro 上运行第二台服务器,然后遇到了这些问题。(下面来自我的终端的错误消息)第二台服务器包括其他一些应用程序,包括 Pihole。我删除了第二个服务器文件并重新启动,问题解决了!接下来是我尝试在 Django 中编辑我的 Views.py 文件,然后当我在虚拟环境文件夹中重新启动我的 Django 服务器时,同样的错误消息又出现了。请参阅以下错误:

mymac@michaels-MacBook-Pro mfdw_root % python manage.py runserver
Traceback (most recent call last):
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/manage.py", line 22, in <module>
    main()
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 786, in exec_module
  File "<frozen importlib._bootstrap_external>", line 923, in get_code
  File "<frozen importlib._bootstrap_external>", line 853, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/mfdw_site/settings.py", line 123
    'DIRS': [BASE_DIR / 'mfdw_site/templates'],
    ^
SyntaxError: illegal target for annotation


end of errors********

这是我在 Stack Overflow 上的第一个问题,我现在非常不确定我是否能让我的服务器在我的 MacBook Pro 上再次运行 Python 3.9.0 和 Django 3.1.3 这是我上面遇到的错误,我可以' t 连接到 127.0.0.1 ,我的 MacBook Pro 上的本地主机。请尝试给我一个关于如何进行的线索。我一直在学习 djangobook.com 的课程。

下面是我的 settings.py 文件:

"""
Django settings for mfdw_site project.

Generated by 'django-admin startproject' using Django 3.1.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '# secret key goes here, it is present in actual code'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'pages.apps.PagesConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mfdw_site.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'mfdw_site/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mfdw_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

'DIRS': [BASE_DIR / 'mfdw_site/templates'],

STATICFILES_DIRS = [BASE_DIR / 'mfdw_site/static')]

另外,我的 Django 安装是否存在路径问题?我在正确更新为使用 Python 3.9 的 MacBook Pro 上运行。我认为这可能与这部分错误有关:

"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()

这看起来合理吗?settings.py 中的标记代码出现在第 123 行。在我看来,python 3.9 有一个包含的 django 安装,它通过路径与 mfdw_project 文件混淆。我是对的,我应该怎么做?

4

1 回答 1

0

请仔细阅读错误,它表示您的设置文件在第 123 行配置错误,并为您提供了与 ^ 违规的行作为其发生位置的标志:

'DIRS': [BASE_DIR / 'mfdw_site/templates'],
^

它说有一个语法错误,所以你可以说问题是你写的东西没有按照python的语法。据此,您应该检查此行之前的行(第 122 行)发生了什么,检查如果将此行放在注释中(带#)会发生什么,它会在下一行发现另一个错误,找到一个稍后某处出错或运行顺利。

如果它在下一行发现错误,你可以检查你在前一行(第 122 行)做错了什么。如果它稍后在某处发现错误,您可以理解问题出在实际行(第 122 行),并且可能是您在那里的 /,列表分隔符是逗号 (,),而不是 /...

编辑:首先,感谢您添加设置文件,它有助于在我们知道要查找的内容后找到错误。违规行是 123,它看起来像这样:

'DIRS': [BASE_DIR / 'mfdw_site/templates'],

这有3个问题:

  1. 'DIRS' 不应该是一个字符串,它应该是一个变量,所以删除 '.
  2. 该行不应该有冒号,这不是字典或函数定义。
  3. 我了解您希望路径为 BASE_DIR/mfdw_site/templates,假设是这样,它可以是 [f'{BASE_DIR}/mfdw_site/templates'] 和其他一些选项,但这是最简单的
于 2020-11-26T12:00:21.847 回答