0

This is my website directory:

django_project
    \bin
    \include
    \lib
    \src
        \django_project
            settings.py
        \app2
        manage.py
    \static
        \js
        \css
        \media
    \templates
        base.html

What I have added to my settings.py is:

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "templates"),
)

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
    STATICFILES_DIRS = (
        os.path.join(os.path.dirname(BASE_DIR), "static"),
    ),

and to my urls.py:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and hey, it does'nt work. The way I refer to them in the base.html is:

<link href="/static/css/bootstrap.min.css" rel="stylesheet">

Any ideas?

Thank you. Hasan

Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78
  • 2
    there are some trailing commas in your code, in the `if DEBUG:` block. Also I don't think the extra urlpatterns are required (I don't use these) – r---------k Oct 13 '14 at 22:12
  • Figured out, and it actually has problems with the trailing commas and directory naming. It's 2 AM and today is my birthday, so I am going to go bed now and past the answer tomorrow :) – Hasan Can Saral Oct 14 '14 at 00:42

3 Answers3

1

Running the settings that you've pasted gave me an error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. Commenting-out os.path.join(os.path.dirname(BASE_DIR), "static"), in the settings removed the error and the template correctly found the CSS file without the URLs.py modifications.

Celeo
  • 5,583
  • 8
  • 39
  • 41
  • I don't get an ImproperlyConfigured error in this configuration. And it doesn't find even if I have the urls.py modification. It does find them when viewing html but not with the development server. But thank you. What should be the proper way to structure my directory? – Hasan Can Saral Oct 13 '14 at 22:14
  • 1
    I have just figured out, and it actually has problems with the trailing commas and the ImproperlyConfigured error is raising because of the directory naming. All other stuff are necessary. It's already 2 AM and today is my birthday, so I am going to go bed now and past the answer tomorrow :) – Hasan Can Saral Oct 14 '14 at 00:43
1

First of all as @r---------k mentioned, I should not have had trailing commas except for the tuples. So the settings.py should look like:

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
    STATICFILES_DIRS = (
        os.path.join(os.path.dirname(BASE_DIR), "static", "static")
    ),

And the second thing is that, as you may notice, STATIC_ROOT and STATICFILES_DIRS cannot possibly have the same value. I added another static folder inside the static folder and put my js and css folders inside of it:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),

The directory looks like:

django_project
    \bin
    \include
    \lib
    \src
        \django_project
            settings.py
        \app2
        manage.py
    \static
        \static
            \js
            \css
        \media
    \templates
        base.html

Finally, you should have the urlpatterns in your urls.py:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78
0

This seems to work in current django (2.2) in your urls.py:

from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path

urlpatterns += [
    path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]

Remember to run ./manage.py collectstatic to collect the static files to settings.STATIC_ROOT

QT-1
  • 900
  • 14
  • 21