问题
So using django for the first time and ran into this issue where the media url does not want to load/work so far my urls.py is setup like so
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),)
my settings.py like so
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/'
my html template like so
<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>
Whenever I type in http://localhost:8000/media/css/template.css I get
AttributeError at /media/css/template.css/
'str' object has no attribute 'resolve'
and in my django server log the following
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
When I type http://localhost:8000/home/ my page loads but none of my css or javascript is loaded..
回答1:
If you're using Django for the first time, you should be using 1.4. If you're using a lesser version, upgrade before going any farther. Starting a new project on an old version of the framework will bite you later.
So, given Django 1.4. You need the following (and only the following):
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
is just a convenience variable to save repetition; it doesn't have anything to do with Django. The "assets" directory is where you will put all your project-wide static resources. You can name that anything you like, it just can't be the same as MEDIA_ROOT
or STATIC_ROOT
.
Also note: MEDIA_ROOT
is now only for uploads, i.e. files added via FileField
s and ImageField
s on your models. STATIC_ROOT
is only for the output of the collectstatic
management command, which you'll only use in production; you never actually store anything there yourself.
If you're using runserver
in development, Django will automatically serve all your static resources for you. Only if you're using another webserver in development, will you need to add the following to urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
Finally, in order to serve your MEDIA_ROOT
directory in development add the following to 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,
}),
)
In production, both MEDIA_ROOT
and STATIC_ROOT
will be served directly by your webserver, not Django.
See: https://docs.djangoproject.com/en/dev/howto/static-files/
回答2:
http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/
Say you are working on a Django project, using its development web server, and you get this exception when you try to load a page in the browser:
AttributeError: 'str' object has no attribute 'resolve'
It's because you forgot to type the word "patterns".
Specifically, in some url.py, you typed something like this:
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?
In the first one, I'm incorrectly assigning urlpatterns to be a tuple. In the second, I'm correctly using the django.conf.urls.defaults.patterns function.
回答3:
us this code
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'media','show_indexes': True}),
)
来源:https://stackoverflow.com/questions/9915392/django-media-not-loading