此处不允许目录索引 Django

问题描述 投票:0回答:4

我有一个 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

这是我的设置.py

import os
# Django settings for uproject project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', '[email protected]'),
)

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
4个回答
0
投票

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

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


0
投票

您的

STATICFILES_DIRS
为空 - 开发服务器将从这里提供服务。


0
投票

您必须编辑 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


0
投票

除了 henrikstroem 的回答:

您应该查看 有关 STATIC_ROOTSTATICFILES_DIRS 的官方文档。

基本上,

STATIC_ROOT
collectstatic
命令将收集所有静态文件的位置,
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)来完成此任务。

在无耻的自我广告中,你可以在这里获取我的项目框架。

© www.soinside.com 2019 - 2024. All rights reserved.