0

I'm currently mad at Django (1.9) right now! The saddest thing is 'Static URL' is the one giving me problem. 'Media URL' is working fine, no problem, but the static url is giving a huge headache.

in my settings_dev.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))  
STATIC_ROOT = os.path.join(PROJECT_PATH,'../static/')
STATIC_URL = '/static/'

when I add the below tag:

{% load static from staticfiles %}

  <script type="text/javascript" src="{% static 'datepicker/js/bootstrap-datepicker.js' %}"></script>

The js file won't load. when I check my source code, it will display the below link.

   <script type="text/javascript" src="/static/datepicker/js/bootstrap-datepicker.js"></script>

And when I click it will redirect me to

  http://127.0.0.1:8000/static/datepicker/js/bootstrap-datepicker.js

And display

  Page not found (404)
  Request Method:   GET
  Request URL:  http://127.0.0.1:8000/static/datepicker/js/bootstrap-  datepicker.js

Now, I adjusted my urls.py to

if settings_dev.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings_dev.MEDIA_ROOT, 'show_indexes': True}),
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings_dev.STATIC_ROOT, 'show_indexes': True}),
 )

Yet, I'm still getting the same error!! Page not found issues.

Project Directory

PROJECT NAME: Book/

SUB DIRECTORY: 
  media
  static
  Template
  book
  bookapp
  manage.py (this is a file)

What am I missing?

smack
  • 922
  • 11
  • 21

2 Answers2

3

Okay to make things clear for you.

STATIC_ROOT is that directory where all your static data gets collected when you want to serve your files on another server, say APACHE or NGINX or maybe on Heroku or so.

If don't want just want to run your web-app on your local development server, you don't require python manage.py collectstatic and hence you don't need STATIC_ROOT.

All you need is STATIC_URL and in case you have your static files at some other location as well then you also need STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),].

So you'll be having a folder named `static' at the base directory level where django will look for your static files.

If you specify the same folder for STATIC_DIRS and STATIC_ROOT then you'll automatically get an error. Django won't allow you to do that as technically you are trying to give the same directory for two different purposes.

See this for a detailed explanation -> Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

Community
  • 1
  • 1
Ankush Raghuvanshi
  • 1,392
  • 11
  • 17
  • Thanks. if that's the case, my STATIC_ROOT = os.path.join(PROJECT_PATH,'../static/') is right then. It should be locating it. there's no need for STATIC_DIRS since I only have one location where my static files are stored. Besides, I'm still in development server. – smack Aug 03 '16 at 16:29
  • still don't know why Django can't locate my static url – smack Aug 03 '16 at 16:29
  • What you should do is create a new folder at the base directory level. Name it say **static_root_folder** . And then in your settings.py do `STATIC_ROOT = os.path.join(BASE_DIR, 'static_root_folder')` – Ankush Raghuvanshi Aug 03 '16 at 16:34
  • your `static` folder and the place where your static data gets collected to be served for a server which is not the python django server, should not be the same. Hence i just asked you to create a new folder and give it whatever name you want to. And then specify that in the STATIC_ROOT. Though again I would say that you don't need a STATIC_ROOT when you are only working on your local development server i.e., the python django server – Ankush Raghuvanshi Aug 03 '16 at 16:37
  • still the same. I created a new folder named static_root, adjusted the STATIC_ROOT = os.path.join(BASE_DIR, 'static_root') and STATIC_URL = '/static_root/' Yet still getting the same page not found. And the url is reflecting in template http://127.0.0.1:8000/static_root/datepicker/js/bootstrap-datepicker.js – smack Aug 03 '16 at 16:47
  • I asked you to keep them different. So I never asked you to change the STATIC_URL to '/static_root/'. Keep STATIC_URL = '/static/' only and STATIC_URL as os.path.join(BASE_DIR, 'static_root'). So that datepicker folder will go in the folder named as 'static' and not 'static_root'. – Ankush Raghuvanshi Aug 03 '16 at 16:53
  • You don't put any of your stuff in the static_root folder by youself. It automatically gets filled when you run python manage.py collectstatic. Its for that purpose only – Ankush Raghuvanshi Aug 03 '16 at 16:56
  • See the settings.py for one of my web-app. Scroll down to the very bottom. -> https://github.com/ankushrgv/moviemap/blob/master/config/settings.py I hope your confusion will be cleared now. – Ankush Raghuvanshi Aug 03 '16 at 16:59
  • thanks for the reply. your settings is the same as mine.. yet it's not working. I even put the url manually STATIC_ROOT= 'C:/Mann/Scripts/cenv/Scripts/book/static/' yet it's not working!! Could it be the url? urls.py?? just tired! – smack Aug 03 '16 at 17:16
  • my settings.py has different values for STATIC_URL, STATIC_ROOT and STATIC_DIRS. So in reply to my this comment, just comment your values of STATIC_URL, STATIC_ROOT and STATIC_DIRS in the order i just mentioned. – Ankush Raghuvanshi Aug 03 '16 at 17:21
  • And even when you are giving the absolute url, then also you are doing it wrong. You are assigning the 'static' folder to STATIC_ROOT, whereas i asked you to assign the folder named 'static_root' to STATIC_ROOT and '/static/' to STATIC_URL. These two are two different things and hence need different folders. That's what i'm trying to tell you in all my comments and the answer a well. Try going through the link i mentioned in my answer. Read it carefully. – Ankush Raghuvanshi Aug 03 '16 at 17:23
1
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]

That line is enough for serving your project static folder files... And you have to set this in your urls.py

urlpatterns = [
    ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
  • STATIC_ROOT only for deployment which means Production server... please see this link ... https://docs.djangoproject.com/en/1.10/howto/static-files/#deployment – Raja Simon Aug 03 '16 at 16:07
  • I'm getting this error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting – smack Aug 03 '16 at 16:13
  • You have to change your STATIC_ROOT to something else... like `STATIC_ROOT='staticfiles'` – Raja Simon Aug 03 '16 at 16:14