繁体   English   中英

Django静态文件没有命名的模块<app_name>

[英]Django Static Files No module named <app_name>

我正在编写一个Django应用程序,我在线部署它,但我遇到了静态文件的一些问题。 当我运行collectstatic命令时,它会显示“没有名为的模块”。 我看过这里的问题,但没有人回答我的问题。

我的静态文件被愚蠢地放入特定于应用程序的目录中,但是我将它们移出并尝试在它们位于正确的目录中时运行它但仍然没有运气。

这很奇怪:

Settings.py:

"""
Django settings for django_project project.

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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.6/howto/deployment/checklist/

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

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ticketr', # App name
)

MIDDLEWARE_CLASSES = (
    '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 = 'django_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '../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 = 'django_project.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '../db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/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.6/howto/static-files/
STATICFILES_DIRS = '/home/django/ticketr/static'
# Tried it at: '/home/django/django_project/django_project/static'
# Also tried it at: '/home/django/static'
STATIC_URL = '/static/'

目录

   .
    ├── db.sqlite3
    ├── django_project
    │   ├── db.sqlite3
    │   ├── django_project
    │   │   ├── __init__.py
    │   │   ├── __init__.pyc
    │   │   ├── settings.py
    │   │   ├── settings.pyc
    │   │   ├── settings.pye
    │   │   ├── urls.py
    │   │   ├── urls.pyc
    │   │   ├── wsgi.py
    │   │   └── wsgi.pyc
    │   └── manage.py
    ├── images
    │   ├── event_images
    ├── media
    │   ├── images
    │   └── qrcode
    ├── qrcode
    ├── templates
    └── ticketr
        ├── admin.py
        ├── admin.pyc
        ├── apps.py
        ├── forms.py
        ├── forms.pyc
        ├── helper.py
        ├── helper.pyc
        ├── __init__.py
        ├── __init__.pyc
        ├── media
        │   └── images
        │       └── event_images
        ├── migrations
        ├── models.py
        ├── models.pyc
        ├── static
        │   ├── 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
        │   ├── fonts
        │   │   ├── glyphicons-halflings-regular.eot
        │   │   ├── glyphicons-halflings-regular.svg
        │   │   ├── glyphicons-halflings-regular.ttf
        │   │   ├── glyphicons-halflings-regular.woff
        │   │   └── glyphicons-halflings-regular.woff2
        │   ├── images
        │   │   ├── background1.png
        │   │   ├── event_images
        │   │   ├── logo.png
        │   │   └── qr.jpg
        │   └── js
        │       └── bootstrap.min.js
        ├── templatetags
        │   ├── __init__.py
        │   ├── __init__.pyc
        │   ├── ticket_extras.py
        │   └── ticket_extras.pyc
        ├── tests.py
        ├── urls.py
        ├── urls.pyc
        ├── views.py
        └── views.pyc

确保将STATICFILES_FINDERS 设置为文档中默认设置 - 这是在应用程序中找到静态文件的内容。 您的静态文件位于应用程序的/static/文件夹中,因此您可以删除STATICFILES_DIRS设置。

您可能还想根据文档添加STATIC_ROOTSTATIC_URL

编辑:你也有一个奇怪的目录结构。 它应该看起来更像https://stackoverflow.com/a/41963237/2532070 -与ticketr第一目录里面django_project目录,在同一水平manage.py

所以基本上我的问题是几件事。 我设置了一个新的Droplet并重新开始,我让它工作:

  1. 我的应用程序位于错误的目录中。 它必须位于与manage.py文件相同的目录中的django_project目录中。
  2. 如上所述,我不得不改变我的STATIC_URL和STATIC_ROOT
  3. 完成后我必须重新启动gunicorn并使用以下命令刷新静态文件:

    python manage.py collectstatic --noinput --clear

    NB。 确保应用程序与django_project位于同一目录中。 home / django / django_project < - 那里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM