多个设置文件阻止我运行服务器(并执行其他命令)

时间:2017-04-10 07:50:53

标签: django

Django 1.11

PyCharm Community Edition 2017.1

我创建了一个virtualenv并激活了它。然后我安装了Django。并根据“Django的两个独立”组织了多个设置文件。

(photoarchive) michael@ThinkPad:~/workspace/photoarchive_project$ tree -I '__pycache__'
.
├── docs
│   └── readme.txt
└── photoarchive
    ├── config
    │   ├── __init__.py
    │   ├── settings
    │   │   ├── base.py
    │   │   ├── __init__.py
    │   │   ├── local.py
    │   │   ├── production.py
    │   │   ├── secrets.json
    │   │   └── utils.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── home
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── __init__.py
    ├── manage.py
    ├── static
    │   └── dist
    │       ├── css
    │       │   ├── bootstrap.css
    │       │   ├── bootstrap.css.map
    │       │   ├── bootstrap.min.css
    │       │   ├── bootstrap.min.css.map
    │       │   ├── bootstrap-theme.css
    │       │   ├── bootstrap-theme.css.map
    │       │   ├── bootstrap-theme.min.css
    │       │   ├── bootstrap-theme.min.css.map
    │       │   ├── ie10-viewport-bug-workaround.css
    │       │   └── main.css
    │       ├── fonts
    │       │   ├── glyphicons-halflings-regular.eot
    │       │   ├── glyphicons-halflings-regular.svg
    │       │   ├── glyphicons-halflings-regular.ttf
    │       │   ├── glyphicons-halflings-regular.woff
    │       │   └── glyphicons-halflings-regular.woff2
    │       └── js
    │           ├── bootstrap.js
    │           ├── bootstrap.min.js
    │           ├── html5shiv.min.js
    │           ├── ie10-viewport-bug-workaround.js
    │           ├── jquery.min.js
    │           ├── npm.js
    │           └── respond.min.js
    └── templates
        ├── base.html
        └── home
            └── home.html

在PyCharm中我添加了解释器〜/ workspace / venv / photoarchive。

在运行/编辑配置中我有:

脚本:/home/michael/workspace/photoarchive_project/photoarchive/manage.py

脚本参数:runserver --settings=config.settings.local

工作目录:/home/michael/workspace/photoarchive_project/photoarchive

好。它从PyCharm运行。

但是当我尝试从命令行启动它时,我得到了这个:

(photoarchive) michael@ThinkPad:~/workspace/photoarchive_project/photoarchive$ python manage.py runserver --settings=config.settings.localPerforming system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fce35181950>
Traceback (most recent call last):
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/michael/workspace/venv/photoarchive/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/home/michael/workspace/photoarchive_project/photoarchive/config/urls.py", line 20, in <module>
    from photoarchive.home.views import HomePageView
ModuleNotFoundError: No module named 'photoarchive'

在urls.py中我有:

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings

from photoarchive.home.views import HomePageView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('registration.backends.hmac.urls')),
    url(r'^$', HomePageView.as_view(), name='home')
]

第20行是“来自photoarchive.home.views import HomePageView”。

然后我尝试了这个:

(photoarchive) michael@ThinkPad:~/workspace/photoarchive_project/photoarchive$ python
Python 3.6.1 (default, Mar 22 2017, 10:05:53) 
[GCC 5.4.1 20170304] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/michael/workspace/venv/photoarchive/lib/python36.zip', '/home/michael/workspace/venv/photoarchive/lib/python3.6', '/home/michael/workspace/venv/photoarchive/lib/python3.6/lib-dynload', '/usr/lib/python3.6', '/home/michael/workspace/venv/photoarchive/lib/python3.6/site-packages']
>>> 

sys.path [0],即'',代表当前目录 (https://docs.python.org/2/library/sys.html#sys.path)。

其他命令(例如makemigrations)也会产生相同的效果。

嗯,我被卡住了。你可以给我一个踢,这样我就可以从命令行运行dev服务器了。并了解其中一些。

稍后添加:

base.py

"""
Django settings for photoarchive project.

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

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

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

import os
from .utils import get_secret

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret('SECRET_KEY')


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
]




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 = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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 = 'config.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': get_secret("DB_NAME"),
        'USER': get_secret("USERNAME"),
        'PASSWORD': get_secret("PASSWORD"),
        'HOST': get_secret("DATABASES_HOST"),
        'PORT': get_secret("PORT"),
    }
}


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

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


# Internationalization
# https://docs.djangoproject.com/en/1.11/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/1.11/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "../static"),
]

local.py

from .base import *

DEBUG = True


# ==== { Django Debug Toolbar
INSTALLED_APPS += ["debug_toolbar", ]
MIDDLEWARE = ['debug_toolbar.middleware.DebugToolbarMiddleware',] + MIDDLEWARE
INTERNAL_IPS = ['127.0.0.1',]
# ==== Django Debug Toolbar }

1 个答案:

答案 0 :(得分:0)

像这个python一样工作manage.py runserver --settings = config.settings.local

并且PyCharm不要强调代码:鼠标右键单击photoarchive目录/ Mark目录为/ Sources Root。