获取django.contrib.staticfiles.views.serve引发的异常
我在Django很天真。在这里尝试一些东西。我在处理静态文件,被困在这里了。请帮帮我 我对static_root和static_dirs非常困惑。我正在学习一个在线教程,其中没有使用静态根目录,并且他们的项目运行良好。如果我尝试,我会面临很多错误和例外 以下是我的看法:获取django.contrib.staticfiles.views.serve引发的异常,django,Django,我在Django很天真。在这里尝试一些东西。我在处理静态文件,被困在这里了。请帮帮我 我对static_root和static_dirs非常困惑。我正在学习一个在线教程,其中没有使用静态根目录,并且他们的项目运行良好。如果我尝试,我会面临很多错误和例外 以下是我的看法: from django.shortcuts import render from django.http import HttpResponse def index(request): my_dict={'inse
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
my_dict={'insert_me':"Hello I am from views.py"}
return render(request,'first_app/index.html',context=my_dict)
以下是我的项目URL:
from django.contrib import admin
from django.conf.urls import url
from django.conf.urls import include
from first_app import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^first_app/',include('first_app.urls')),
url(r'^admin/', admin.site.urls),]
urlpatterns += staticfiles_urlpatterns()
以下是我的应用程序url:
from django.conf.urls import url
from first_app import views
from django.contrib import admin
urlpatterns= [
url(r'', views.index, name='index'),]
index.html:
{% load staticfiles%}
<html>
<head>
<meta charset="utf-8">
<title>DJ Page</title>
</head>
<body>
<h1>Hello this is index.html!</h1>
<img src="{% static 'images/dj.jpg'%}" alt='oh oh! did not show'>
</body>
</html>
点击url时:
http://127.0.0.1:8000/static/images/dj.jpg
我面临以下错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/images/dj.jpg
Raised by: django.contrib.staticfiles.views.serve
'images\dj.jpg' could not be found
您看到此错误是因为Django设置文件中的DEBUG=True。将其更改为False,Django将显示一个标准的404页面。我认为您应该删除
STATICFILES\u DIR
和STATIC\u ROOT
STATIC_URL = '/static/'
STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT=os.path.join(BASE_DIR,'static_media')
静态文件\u DIR-设置是告诉Django要扫描哪些目录中的静态文件
STATIC\u ROOT-设置是在执行
python manage.py collectstatic
命令时告诉Django将所有静态文件放在哪里。您可以共享您的模板index.html
?@Aurélien这是我的模板index.html代码:{%load staticfiles%}DJ页面您好,这是index.html!您的图像
目录在哪里?
STATIC_URL = '/static/'
STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT=os.path.join(BASE_DIR,'static_media')