简体   繁体   中英

Static Files With Django Development Server 1.7 Slightly Different Directory Settings

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

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM