Django 1.4 serving the MEDIA_URL and STATIC_URL files on the development server

It is simply upgraded to Django 1.4 and has serious problems with the new "improved" service of static and multimedia files on the development server. I love Django, but why they made these files doubly complicated with STATIC_URL, STATIC_ROOT, STATICFILES_DIR now completely outside of me.

I'm just trying to serve all files, static and uploaded, on a development server. I have STATIC_URL files working after many experiments, but I just can't get the MEDIA_URL files to be served as well.

Settings:

DIRNAME = os.path.dirname(__file__) MEDIA_ROOT = os.path.join(DIRNAME, 'media/') MEDIA_URL = '/media/' STATIC_ROOT = '' STATIC_URL = '/static/' STATICFILES_DIRS = () 

I have added media and static context processors:

 TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", 'django.core.context_processors.media', 'django.core.context_processors.static', "django.core.context_processors.request", 'satchmo_store.shop.context_processors.settings', 'django.contrib.messages.context_processors.messages', 

)

and I added in url confs:

 # serve static and uploaded files in DEV urlpatterns += staticfiles_urlpatterns() urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

with two conf settings added in the documents, first for static, and second for media.

my structure, the site being the application and the static dir hosted inside it, as pointed out by djangoproject:

 <myproject> --media --settings --templates --website |->static 

In templates, I can serve static content without problems with

 {{STATIC_URL}}css/style.css 

But any uploaded image used by the photologist is not served, but the URLs are correct:

 /media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg 

This directory structure exits from /

Super, super confused. Now everything seems so ridiculously complicated, while I never had any problems.

+4
source share
3 answers

I am very new to Django and I have never had problems with static content.

This is my configuration. Hope this helps

My folder structure

 django-project --mainapp ----settings.py ----wsgi.py ----[...] --otherapp --fixtures --static --templates --manage.py --requirements.txt 

settings.py

 import os, socket DEBUG = True TEMPLATE_DEBUG = DEBUG MAIN_APP = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP, "..")) MY_LOCALHOST = "VirusVault.local" # this is the real name of my local machine :) try: HOST_NAME = socket.gethostname() except: HOST_NAME = "localhost" 

[...]

 if HOST_NAME == MY_LOCALHOST: STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/') STATIC_URL = "/static/" MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media/') MEDIA_URL = "/media/" else: STATIC_ROOT = "/server/path/to/static/files" STATIC_URL = "http://server.com/static/" MEDIA_ROOT = "/server/path/to/static/files/media/" MEDIA_URL = 'http://server.com/static/media/' 

You need to add 'django.contrib.staticfiles' to INSTALLED_APPS

urls.py

 if settings.HOST_NAME == settings.MY_LOCALHOST: urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), ) urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}), ) 
+2
source

I intended to write a comment first, but somehow the add comment button does not work.

Have you checked the rights to the media catalog?

Since that was the answer, I am going to reset one of my perfectly working Django 1.4 site configurations.

Structure:

 -myproject -- media -- static -- templates -- myproject --- settings.py --- urls.py 

settings.py:

 PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) # Not the best way but works MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/') MEDIA_URL = '/media/' STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/') STATIC_URL = '/static/' 

urls.py:

 from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns = patterns('', ... (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), ) urlpatterns += staticfiles_urlpatterns() 
0
source

If you use the Django 1.4 folder structure, you have moved settings.py to the new folder of the website application, which means that your MEDIA_ROOT is now invalid. Not sure if relative location works in this case, but there should be something like this

 MEDIA_ROOT = os.path.join(DIRNAME, '../media/') 

It may be easier to use an absolute path.

0
source

All Articles