【问题标题】:Django Static Files No module named <app_name>Django 静态文件没有名为 <app_name> 的模块
【发布时间】:2017-09-16 06:52:48
【问题描述】:

我正在编写一个 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

【问题讨论】:

    标签: python django


    【解决方案1】:

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

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

      python manage.py collectstatic --noinput --clear

      注意。确保应用程序与 django_project 位于同一目录中。 home/django/django_project

    【讨论】:

      【解决方案2】:

      确保将STATICFILES_FINDERS 设置为default setting in the docs - 这就是在您的应用中查找静态文件的方法。您的静态文件位于应用内的 /static/ 文件夹中,因此您可以删除您的 STATICFILES_DIRS 设置。

      您可能还想为每个the docs 添加STATIC_ROOTSTATIC_URL

      编辑:您还有一个奇怪的目录结构。它应该看起来更像https://stackoverflow.com/a/41963237/2532070 - 在第一个django_project 目录中包含ticketr 目录,与manage.py 处于同一级别。

      【讨论】:

      • 不行不行。我添加了静态文件查找器,我认为我不需要它们,因为 django.contrib.staticfiles 已经包含在内。但是我将静态根目录更改为它在我的应用程序中的目录,它仍然无法正常工作。
      • @MarkBarrett STATIC_ROOT 应该是一个新目录,例如 ./static/ 收集文件的地方......而不是收集静态文件的地方。 Collectstatic 在您指定的所有目录中查找静态文件,并将它们收集到一个位置。
      • @MarkBarrett 我猜问题是我的编辑,你的目录结构。
      • 是的,解决了它。它收集静态文件,但所有静态文件都是 404
      • @MarkBarrett 这是一个单独的问题,但最好的解决方案可能是查看 404 的 URL 是什么,并确保您的 STATIC_URLSTATIC_ROOT 一起工作以将文件指向那里。
      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2013-07-17
      • 2023-03-26
      • 2012-06-02
      相关资源
      最近更新 更多