2

I am using Django 1.4 and for some reason i am able to serve media files, but not the static ones... Here is my code: settings:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

urls.py:

(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
                    (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),

base.html:

<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet">
<link href="{{ MEDIA_URL }}css/bootstrap-colorpicker.css" rel="stylesheet">

i get a 404 http not found... what am i doing wrong? An i did create the static folder in my project right next to media

http://mysite.com:8000/static/css/bootstrap.css

psychok7
  • 5,373
  • 9
  • 63
  • 101

3 Answers3

1

I suggest removing the explicit media and static views and allowing the staticfiles app to create them (when DEBUG is True under development).

Check the default finders are present in your settings.py https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS

Either add your project static directory to STATICFILES_DIRS (https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS) or place app specific static folders under each app. The app needs to be listed in the INSTALLED_APPS for the finders to locate the static content.

Do not place static files into STATIC_ROOT yourself. This directory is managed by the collectstatic command. See https://docs.djangoproject.com/en/dev/howto/static-files/#deployment

AndrewS
  • 1,666
  • 11
  • 15
  • default finders are present. i tried adding os.path.join(BASE_DIR, 'static/'), to STATICFILES_DIRS with no luck.. is that what you are suggesting? i want to keep my static files in one place only like the projects directory not inside the apps because i have some common resources – psychok7 May 20 '13 at 14:34
  • im not sure i understood.. i don't have a view for /static.. only the routing in urls and the rest in my settings.py – psychok7 May 20 '13 at 14:51
  • Remove the static entry from urls.py. Also, 'STATIC_ROOT' should not be where your project static files are kept. The 'STATIC_ROOT' directory is where the 'collectstatic' command will place all static content for serving in production. – AndrewS May 20 '13 at 14:54
  • i did that and it only creates the admin static files.. it seems like it does not recognize my other static files for some reason – psychok7 May 20 '13 at 15:04
  • What is the full path to the root of the project's static files? That is the path you add to STATICFILES_DIRS. It should **not** be the same as STATIC_ROOT. As this is a directory outside of an app you don't need to add it to INSTALLED_APPS. Running collectstatic will copy all known static content to the STATIC_ROOT so it's a good test to see what is being found as static content. During development you don't serve files from the STATIC_ROOT and should use the staticfiles app to serve them automatically. There's no need to add the static URL to urls.py. – AndrewS May 20 '13 at 15:16
1

Your static folder should be under one app that you use it for.

For example, I have a project named my_project and an application named my_app, I have some static files used in my_app so I put them under ~/project_path/my_project/my_app/static

NB: my_app must be in INSTALLED_APPS. See STATICFILES_FINDERS documentation.

Edit:

As a best practice, you should have a global static folder in one app (the main one), for example a static folder how contains your html template basic resources as jquery, bootstrap, your global style.

And for the static files how's required only for one app, for example app foo, these files should be under foo/static folder

Mounir
  • 11,306
  • 2
  • 27
  • 34
  • what if i need the same static file for 2 or more apps?? i duplicate the file or collect-static automatically detects and only uses 1? – psychok7 May 20 '13 at 15:53
  • 1
    yeah it use only one file, like @Andrew S say, we have used collectstatic to test if Django find or not the static folder, but it's used only for production(for example for apache). If in the future you would to delete one apps, it can't infect your others apps because any apps is static folder is independant from each other, that's the trick ! – Mounir May 20 '13 at 16:03
  • Thanks - had spent an hour trying to debug this. – Ian Carpenter Jul 22 '14 at 21:35
0

Here is how i define my media url in settings.py:

import os
ABSOLUTE_PATH           = os.path.dirname(__file__)
MEDIA_ROOT              = os.path.join(ABSOLUTE_PATH, '../media')
MEDIA_URL               = '/media/'
STATIC_ROOT             = os.path.join(ABSOLUTE_PATH, '../static')
STATIC_URL              = "/static/"

So like you see the difference is ../media and ../static

Is mysite in your installed apps ? Django look inside your installed apps and check if there's a static folder there.

Mounir
  • 11,306
  • 2
  • 27
  • 34
  • Where is the location of your static folder ? – Mounir May 20 '13 at 14:16
  • /home/username/git/mysite/mysite/static – psychok7 May 20 '13 at 14:21
  • Is your settings.py here /home/username/git/mysite/mysite/settings.py ? and /home/username/git/mysite your project folder ? – Mounir May 20 '13 at 14:24
  • Another think, verify that you have this in your settings file: `STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', )` – Mounir May 20 '13 at 14:25
  • its here /home/username/git/mysite/mysite/settings.py – psychok7 May 20 '13 at 14:28
  • and yes i have the staticfiles_finder like that – psychok7 May 20 '13 at 14:28
  • Run this command `./manage.py collectstatic` and check if your files are copied here /home/username/git/mysite/static/ – Mounir May 20 '13 at 14:32
  • only the admin static files were copied to /home/username/git/mysite/mysite/static/ – psychok7 May 20 '13 at 14:38
  • So Django doesn't know that you have a static files there. Is mysite in your installed apps ? – Mounir May 20 '13 at 14:42
  • No its not, only the apps.. but i tried adding now and it gives Error: No module named mysite – psychok7 May 20 '13 at 14:48
  • It's the error, Django look inside your installed apps and check if there's a static folder there. So could you post your settings.py and check if you have a `__init__.py` file inside /home/username/git/mysite/mysite – Mounir May 20 '13 at 14:51
  • yes i have __init__.py there.. i could put my static files under my apps, but at this point i have them all together in one folder and its being shared accross multiple apps – psychok7 May 20 '13 at 15:06