因此,第一次使用 django 并遇到了媒体 url 不想加载/工作的问题,到目前为止我的 urls.py 设置如下
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),)
我的 settings.py 像这样
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = 'http:/localhost:8000/admin-media/'
我的html模板像这样
<link rel="stylesheet" href="/media/css/template.css" type="text/css" />
<link rel="stylesheet" href="/media/css/nivo-slider.css" type="text/css" />
<script type="text/javascript" src="/media/js/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="/media/js/jquery.nivo.slider.pack.js"></script>
每当我输入http://localhost:8000/media/css/template.css我得到
AttributeError at /media/css/template.css/
'str' object has no attribute 'resolve'
并在我的 django 服务器中记录以下内容
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 283, in run
self.result = application(self.environ, self.start_response)
File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 272, in __call__
response = self.get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 218, in handle_uncaught_exception
return callback(request, **param_dict)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\defaults.py", line 30, in server_error
t = loader.get_template(template_name) # You need to create a 500.html template.
File "C:\Python27\lib\site-packages\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html
当我输入http://localhost:8000/home/我的页面加载但我的 css 或 javascript 都没有加载..