此处不允许目录索引Django

j91ykkif  于 2022-09-26  发布在  Go
关注(0)|答案(4)|浏览(281)

我有一个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', '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/时,出现e1d1e错误。
知道吗?我认为提供静态文件的方式有一些错误。顺便说一下,我正在使用extjs。
我在等索引。html在static/updater/目录中显示。这是默认的吗?

wkyowqbh

wkyowqbh1#

由于某些原因,Django似乎不想提供/static/updater/的目录列表。因为静态更新程序是一个目录,而不是一个文件,所以请尝试执行/static/updater/index。html或只是/static/index。html并查看是否提供了其中一个。如果不提供服务,则可能是静态文件设置有问题。如果是这样,那么我不确定是Django拒绝在静态文件夹中提供目录索引还是其他原因。
(这看起来应该是一个评论,但我没有足够的代表来评论。)

smdnsysy

smdnsysy2#

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

c0vxltue

c0vxltue3#

您必须编辑URL。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中的说明来显示索引。提供您喜欢的html文件并将其添加到static/directoryindex.html

csbfibhn

csbfibhn4#

除了henrikstroem的回答:
您应该看看关于STATIC_ROOT和STATICFILES_DIRS的官方文档。
基本上,STATIC_ROOT是e1d1e命令将所有静态文件收集到的位置,而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,并在STATICFILES_DIRS中包含project_root/static。如您所见,我将静态资产存储在project_root/static中,然后它们就可以得到服务了。
在开发期间(意思是:当使用runserver命令时),您的静态资产将直接从它们的位置得到服务,在我的例子中是project_root/static。部署时,您将把静态资产收集到STATIC_ROOT。这可以是您项目中的一个文件夹(作为我的project_root/run/static,甚至是一个完全不同环境中的目录,您甚至可以将其复制到CDN[Content Delivery Network])。
就个人而言,我喜欢用Apache部署。我将设置一个虚拟环境来处理Django部件。静态资产由另一个虚拟环境提供服务,它完全绕过所有动态处理,只提供静态文件。您甚至可以使用其他服务器(如nginx)来执行此任务。
在无耻的自我广告中,你可以找到我的项目框架here

相关问题