这里不允许使用目录索引Django

时间:2015-06-27 20:16:17

标签: python django extjs django-staticfiles

我有一个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能够显示出来。它会默认发生吗?

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

除了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 [Content Delivery Network])。

就个人而言,我喜欢使用Apache进行部署。我将设置一个virtualenv来处理Django部分。静态资产由另一个virtualenv提供服务,完全绕过所有动态处理并简单地提供静态文件。您甚至可以使用其他服务器(如nginx)执行此任务。

在无耻的自我宣传中,您可以获取我的项目框架here

答案 3 :(得分: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