Skip to content Skip to sidebar Skip to footer

Django Media Not Loading

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 += patte

Solution 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 FileFields and ImageFields 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/


Solution 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.


Solution 3:

us this code

urlpatterns += patterns('',
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'media','show_indexes': True}),
    )

Post a Comment for "Django Media Not Loading"