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
django 不能访问静态资源的解决办法的更多相关文章
- [刘阳Java]_SpringMVC访问静态资源_第9讲
有些时候我们在使用SpringMVC的时候造成无法访问静态资源文件(如:html,js,css,image等等).其主要的原因出在web.xml文件我们设置SpringMVC前端控制器的映射路径 &l ...
- nodejs 构建本地web测试服务器 以及 解决访问静态资源的问题!
直接打开html文件,是以file:///方式打开的,这种方式很多时候会遇到跨域的问题,因此我们一般会搭建一个简易的本地服务器,来运行测试页面. 一.构建静态服务器 1.使用express模块 建立个 ...
- django 访问静态资源
urlpatterns = patterns('', url(r'^$', views.show, name='index'), url(r'^static/(?P<path>.*)', ...
- 解决SpringBoot页面跳转无法访问静态资源的问题
初学SpringBoot,写项目的时候遇到了问题,原本的页面是这样的 但启动项目后是这样的 这是因为thymeleaf中引入静态资源及模板需要使用到 th:xxx 属性,否则无法在动态资源中访问静态资 ...
- SpringMVC使用可以访问静态资源,但是导致Controller访问失败
如果在web.xml 拦截配置如下: <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC&l ...
- spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...
问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...
- SpringMVC访问静态资源的三种方式(转)
本文转自:http://www.iigrowing.cn/springmvc_fang_wen_jing_tai_zi_yuan_de_san_zhong_fang_shi.html 如何你的Disp ...
- 用node.js express设置路径后 子路径下的页面访问静态资源路径出问题
在routes/news_mian.js 设置了访问news_main.html 的路径 '/',通知设置一个访问news-page.html的子路径'/newspage'子路径.但是在访问loacl ...
- SpringBoot读取war包jar包Resource资源文件解决办法
SpringBoot读取war包jar包Resource资源文件解决办法 场景描述 在开发过程中我们经常会碰到要在代码中获取资源文件的情况,而我在最近在SpringBoot项目中时碰到一个问题,就是在 ...
随机推荐
- 洛谷 P2542 [AHOI2005]航线规划 解题报告
P2542 [AHOI2005]航线规划 题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel星系 ...
- bzoj4152 The Captain (dijkstra)
做dijkstra,但只需要贪心地把每个点连到它左边.右边.上边.下面的第一个点就可以了 #include<bits/stdc++.h> #define pa pair<int,in ...
- springmvc接收数组类型参数
直接在方法上使用List<Bean>报不能实例化错误! 必须将List<Bean>放在某个对象里作为属性方可接收.具体见如下例子 前端代码 <!DOCTYPE html& ...
- IOI2018题解
只有部分题解 练习赛 T2 自然还是要简单考虑了 0~n-1的排列,考虑相对的大小 我们先考虑对于前三个:a,b,c 询问a,b,询问b,c,再询问a,b,c 发现,如果三个知道两个,那么第三个可以唯 ...
- python之旅:元类
type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...
- Json对象与Json字符串
- OpenStack 认证服务 KeyStone 服务注册(五)
创建服务实体和API端点 创建服务 openstack service create --name keystone --description "OpenStack Identity&qu ...
- node.js原生后台进阶(二)
上一章讲到怎么样用原生node.js来获取GET.POST(urlencoded,formData)的参数,这一次我们更进一步,讲一下以下的点: 1.压缩(zlib) 2.流(stream) 3.路由 ...
- mybatis+sqlserver中返回非自增主键
首先把实体类贴出来(这里只贴出属性,其它的就是getter和setter方法): public class Around { private String xccd; //对应主键 ...
- 一个ssm综合小案例-商品订单管理----写在前面
学习了这么久,一直都是零零散散的,没有把知识串联起来综合运用一番 比如拦截器,全局异常处理,json 交互,RESTful 等,这些常见技术必须要掌握 接下来呢,我就打算通过这么一个综合案例把这段时间 ...