zoukankan      html  css  js  c++  java
  • django 不能访问静态资源的解决办法

    最近在中文win10下使用python的django搭建web测试服务器,发现一个诡异的现象,正常配置好django的模型,视图和模板,

    1.setting.py内容如下:

    """
    Django settings for test4 project.

    Generated by 'django-admin startproject' using Django 1.8.3.

    For more information on this file, see
    https://docs.djangoproject.com/en/1.8/topics/settings/

    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/1.8/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.8/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = ')j2-td!h@f=*(cb3htaifp19u47j6l8=ky*lumq!q7*#=uu1-9'

    # SECURITY WARNING: don't run with debug turned on in production!
    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',
    'booktest',
    )

    MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    )

    ROOT_URLCONF = 'test4.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 = 'test4.wsgi.application'

    # Database
    # https://docs.djangoproject.com/en/1.8/ref/settings/#databases

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': "mysql",
    'USER': "root",
    "PASSWORD": "mysql",
    "HOST": "localhost",
    "PORT": 3306,
    }
    }

    # Internationalization
    # https://docs.djangoproject.com/en/1.8/topics/i18n/

    LANGUAGE_CODE = 'zh-hans'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True

    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.8/howto/static-files/

    STATIC_URL = '/static/'
    STATICFILES_DIR = [os.path.join(BASE_DIR, 'static'), ]

    2.应用booktest的views.py
    from django.shortcuts import render

    # Create your views here.
    def index(request):
    return render(request,"booktest/index.html")

    3.应用booktest的urls.py内容如下:
    from django.conf.urls import url
    from booktest import views

    urlpatterns = [
    url(r'^test/', views.index),
    ]

    4.模板文件index.html
    <!DOCTYPE html>
     {% load staticfiles %}
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>静态文件显示演示</title>
    </head>
    <body>
    <h1>这是一个演示图片</h1>
    <img src="/static/images/test.JPG"><br/>
    <img src={% static 'images/test.JPG' %}><br/>
    </body>
    </html>

    工程结构如下:

    工程test4下urls.py的内容如下:

    from django.conf.urls import include, url
    from django.contrib import admin


    urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include("booktest.urls"))
    ]
    
    


    然后通过通过浏览器http://localhost/test/访问,总是在后端提示404错误
    后来无意发现把静态资源文件挪到应用booktest目录下即可正常访问静态资源了,当然也可直接http://localhost/static/images/test.jpg进行访问了
    但是直接使用http://localhost/static/不能访问,在后端提示“Directory indexes are not allowed here.”
    这个问题希望网友能提供解决办法,谢谢。

    我使用的python3.6,djanggo版本是1.8.3


  • 相关阅读:
    计算机网络基础
    ansible
    CDH集群日常
    漏洞挖掘学习
    JDWP
    开源安全项目调研
    SMB漏洞汇总
    Windows账户LM/NTLM
    SMB知识汇总
    Memcache未授权漏洞
  • 原文地址:https://www.cnblogs.com/it-tsz/p/10247496.html
Copyright © 2011-2022 走看看