Django - Static file not found

前端 未结 13 841
旧巷少年郎
旧巷少年郎 2020-11-30 20:46

I\'ve seen several posts for this issue but didn\'t found my solution.

I\'m trying to serve static files within my Django 1.3 development environment.

Here a

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 21:01

    Serving static files can be achieved in several ways; here are my notes to self:

    • add a static/my_app/ directory to my_app (see the note about namespacing below)
    • define a new top level directory and add that to STATICFILES_DIRS in settings.py (note that The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting)

    I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file admin-custom.css to override a couple of admin styles, I have a setup like so:

    .
    ├── my_app/
    │   ├── static/
    │   │   └── my_app/
    │   │       └── admin-custom.css
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── static/
    ├── templates/
    │   └── admin/
    │       └── base.html
    └── manage.py
    
    # settings.py
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATIC_URL = '/static/'
    

    This is then used in the template like so:

    # /templates/admin/base.html
    {% extends "admin/base.html" %}
    {% load static %}
    
    {% block extrahead %}
        
    {% endblock %}
    

    During development, if you use django.contrib.staticfiles [ed: installed by default], this will be done automatically by runserver when DEBUG is set to True [...]

    https://docs.djangoproject.com/en/1.10/howto/static-files/

    When deploying, I run collectstatic and serve static files with nginx.


    The docs which cleared up all the confusion for me:

    STATIC_ROOT

    The absolute path to the directory where collectstatic will collect static files for deployment.

    ...it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).

    https://docs.djangoproject.com/en/1.10/ref/settings/#static-root


    Static file namespacing

    Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

    https://docs.djangoproject.com/en/1.10/howto/static-files/


    STATICFILES_DIRS

    Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

    https://docs.djangoproject.com/en/1.10/howto/static-files/

提交回复
热议问题