我一定错过了什么。
所以这应该是我的文件夹看起来的样子:
http://oi60.tinypic.com/2q1cvft.jpg
显然有问的形象。
http://oi61.tinypic.com/r01s1k.jpg
为了提高可移植性,我在代码中使用动态路径。
settings.py
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
STATIC_PATH,
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango'
urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),url(r'^rango/', views.about, name='about'))
views.py
def index(request):
context = RequestContext(request)
# Construct a dictionary to pass to the template engine as its context.
# Note the key boldmessage is the same as {{ boldmessage }} in the template!
context_dict = {'boldmessage': "I am bold font from the context"}
# Return a rendered response to send to the client.
# We make use of the shortcut function to make our lives easier.
# Note that the first parameter is the template we wish to use.
return render_to_response('rango/index.html', context_dict, context)
def about(request):
return HttpResponse("Rango says: This is about page!<a href='../../'>Index</a>")
请注意,图像放在html模板上。 html是好的(加载),但图像不。
<!DOCTYPE html>
{% load static %} <!-- New line -->
<html>
<head>
<title>Rango</title>
</head>
<body>
<h1>Rango says...</h1>
hello world! <strong>{{ boldmessage }}</strong><br />
<a href="/rango/about/">About</a><br />
<img src="{% static "rango.png" %}" alt="Picture of Rango" /> <!-- New line -->
</body>
</html>
结果
http://oi58.tinypic.com/v8fqe8.jpg
图片由讨厌的404
从127.0.0.1:8000/static/rango.png
或manage.py findstatic rango.png
进行访问会导致失败。
但是,从file:///D:/tango_with_django_project/static/rango.png
访问将会成功。
由于
有条件的信息:
DEBUG=TRUE
我检查了STATIC_PATH
,文件确实导致了正确的路径D:\tango_with_django_project\static
是否可能由于反斜杠和正斜杠,但如果斜杠是问题,那么模板也应该被破坏。
答案 0 :(得分:3)
在生产环境中,静态文件应由Web服务器提供,而不是由django本身提供。这就是为什么默认情况下不启用它。
您需要向开发服务器说它必须提供静态文件。我认为在DEBUG=True
中设置settings.py
就足够了。
您可能还需要在urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'your_app.views.home', name='home'),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls))
)
if settings.DEBUG :
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)$', 'serve'),
)
我希望它有所帮助
答案 1 :(得分:0)
问题可能与应用程序目录缺少查找程序有关。将以下内容添加到项目的setting.py:
中STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)