【问题标题】:Directory indexes not allowed here Django这里不允许目录索引Django
【发布时间】:2015-09-14 14:00:13
【问题描述】:

我有一个 django 应用程序。这是我的目录结构。

.
+--media
   +--index.html
+--static
   +--extjs
      +--<extjs files>
   +--updater
      +--app
      +--images
      +--ux
      +--app.js
      +--index.html
   +--index.html
+--templates
   +--<template files>
+--uapp
   +--__init__.py
   +--models.py
   +-- <etc>
+--manage.py
+--settings.py
+--urls.py

这是我的 settings.py

import os
# Django settings for uproject project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'updates',                      # Or path to database file if using sqlite3.
        'USER': '<user>',                      # Not used with sqlite3.
        'PASSWORD': '<password>',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Los_Angeles'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Path to project installation.
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
#MEDIA_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/updater/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = "/static/"

# Additional locations of static files
STATICFILES_DIRS = (
    #os.path.join(SITE_ROOT, 'static'),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)


# Make this unique, and don't share it with anybody.
SECRET_KEY = 'hnu51442$#@gdsvx5v!61w^4-vjevy8xm6tqb56#bc216!nw-nl-%'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'urls'

# WSGI_APPLICATION = 'wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(SITE_ROOT, 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'haystack',
    'uapp',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
    },
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

LOGIN_URL = '/updater/uapp/login/'
LOGOUT_URL = '/updater/uapp/logout/'
LOGIN_REDIRECT_URL = '/updater/media/updater/'
CACHE_BACKEND = 'db://listclient_cache'

当我转到 localhost:8000/static/updater/ 时,我收到 Directory Indexes not allowed here 错误。

有什么想法吗?我认为提供静态文件的方式存在一些错误。顺便说一句,我正在使用 extjs。

我期待 static/updater/ 目录中的 index.html 出现。默认情况下会发生吗?

【问题讨论】:

    标签: python django extjs django-staticfiles


    【解决方案1】:

    除了 henrikstroem 的回答:

    您应该看看official documentation on STATIC_ROOTSTATICFILES_DIRS

    基本上,STATIC_ROOTcollectstatic 命令将收集所有静态文件的位置,STATICFILES_DIRS 是该命令将查找静态文件的位置(或者是位置)。

    我的开发设置如下所示:

    project_root
        + apps
        + project
            __init__.py
            settings.py
            urls.py
            wsgi.py
        + run
            dev.sqlite3
            + media
            + static
        + static
        + templates
    

    我将STATIC_ROOT 指向project_root/run/static,并将project_root/static 包含在STATICFILES_DIRS 中。 如您所见,我将我的静态资产存储在 project_root/static 中,然后它们就会得到服务。

    在开发期间(意思是:在使用runserver 命令时),您的静态资产将直接从它们的位置提供服务,在我的情况下为project_root/static。部署时,您会将静态资产收集到STATIC_ROOT。 这可以是您项目中的一个文件夹(如我的project_root/run/static 甚至是完全其他环境中的目录,您甚至可以将它们复制到 CDN [内容交付网络])。

    就个人而言,我喜欢使用 Apache 进行部署。我将设置一个 virtualenv 来处理 Django 部分。静态资产由另一个完全绕过所有动态处理并仅提供静态文件的 virtualenv 提供服务。你甚至可以使用另一个服务器,比如 nginx,来完成这个任务。

    在无耻的自我广告中,你可以获取我的项目骨架here

    【讨论】:

      【解决方案2】:

      您必须编辑您的 urls.py 并将这些行添加到其中,然后转到 django.views.static.serve 并将 show_indexes 参数更改为 True。 添加urls.py:

      from django.contrib.staticfiles.urls import staticfiles_urlpatterns
      #import this at top
      url(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root':"path\to\your\static\folder"}),
      #add this to urlpatterns variable
      urlpatterns += staticfiles_urlpatterns()
      #add this in last line of urls.py
      

      现在你完成了,django 显示索引 如果您想更改它的 html 视图,您必须按照 django.views.static.serve 中的说明,并将您喜欢的 html 文件添加到 static/directory_index.html

      【讨论】:

        【解决方案3】:

        您的 STATICFILES_DIRS 为空 - 开发服务器将从此处提供服务。

        【讨论】:

          【解决方案4】:

          似乎出于某种原因 Django 不想为 /static/updater/ 的目录列表提供服务。因为静态更新程序是一个目录,而不是一个文件,所以尝试做 /static/updater/index.html 或只是 /static/index.html 看看是否提供了任何一个。如果未提供它们,则可能是您的静态文件设置存在问题。如果是这样,那么我不确定它是否只是 Django 拒绝在静态文件夹或其他东西中提供目录索引。

          (感觉应该是评论,但我没有足够的代表评论。)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多