[英]django media not loading
所以第一次使用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。
如果您是第一次使用Django,則應該使用1.4。 如果您使用的是較低版本,請先升級,然后再升級。 在舊版本的框架上啟動新項目將在以后給您帶來麻煩。
因此,鑒於Django 1.4。 您需要以下內容(並且僅以下內容):
PROJECT_ROOT = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'assets'),
)
PROJECT_ROOT
只是一個方便變量,可以節省重復; 它與Django沒有任何關系。 在“ assets”目錄中,您將放置所有項目范圍的靜態資源。 您可以隨意命名,不能與MEDIA_ROOT
或STATIC_ROOT
相同。
另請注意: MEDIA_ROOT
現在僅用於上傳,即通過模型上的FileField
和ImageField
添加的文件。 STATIC_ROOT
僅用於collectstatic
管理命令的輸出,該命令僅在生產中使用; 您從來沒有真正在自己那里存儲任何東西。
如果您在開發中使用runserver
,則Django會自動為您提供所有靜態資源。 僅在開發中使用其他Web服務器時,才需要在urls.py中添加以下內容 :
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
最后,為了在開發中提供MEDIA_ROOT
目錄,請將以下內容添加到urls.py中:
from django.conf import settings
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
在生產中, MEDIA_ROOT
和STATIC_ROOT
都將直接由您的網絡服務器(而不是Django)提供。
參見: https : //docs.djangoproject.com/en/dev/howto/static-files/
http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/
假設您正在使用其開發Web服務器來處理Django項目,並且嘗試在瀏覽器中加載頁面時會遇到此異常:
AttributeError: 'str' object has no attribute 'resolve'
這是因為您忘記輸入單詞“ patterns”。
具體來說,在某些url.py中,您鍵入了以下內容:
urlpatterns = ('', (r'^$', direct_to_template, {'template':'a.html'}), # ... when you should have typed this: urlpatterns = patterns('', (r'^$', direct_to_template, {'template':'a.html'}), # ... See the difference?
在第一個中,我錯誤地將urlpatterns分配為一個元組。 第二,我正確地使用了django.conf.urls.defaults.patterns函數。
我們這個代碼
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'media','show_indexes': True}),
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.