Django static Files

后端 未结 3 1289
广开言路
广开言路 2020-12-14 13:57

I am trying to develop a website with Django following various guides, the documentation and google. I have some flat pages set up that need access to images/css files.

相关标签:
3条回答
  • 2020-12-14 14:02

    Django is an fantastic system. Feature rich and yet still simple, elegant & extremely well documented, with excellent examples.....for everything EXCEPT static content. We struggle with this on every new Django project. But we recently solved this, in what we believe is the "recommended" way.

    The key bit of knowledge to keep in mind is that Django does not want to directly serve static content. It would prefer you offload this to the underlying webserver (such as Apache). There are ways to force Django to serve it for you -- and you might even think it's a good idea to have all serving done in one common platform. But take my word for it -- you'll regret this later, and wish you'd just followed the Django recommendation on this one.

    Short story:

    These settings go in 'settings.py'

    Set STATIC_ROOT to the full file system path to where you wish to keep your static content. I recommend somewhere outside of your regular web-application directories. Remember, Apache will serve this directly and you don't want to open a security issue under your feet.

    Set STATIC_ROOT = '/usr/local/static/MyProject/' # include trailing slash

    Set STATIC_URL = '/static/' # no reason to get too fancy with this

    Set ADMIN_MEDIA_PREFIX = '/static/admin/' # don't forget this one

    Make sure the default STATICFILES_FINDERS is set:

    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        )
    

    And make sure that the INSTALLED_APPS setting includes 'django.contrib.staticfiles'

    And make sure that TEMPLATE_CONTEXT_PROCESSORS includes 'django.core.context_processors.static'

    ---- NEXT ----

    You will want to configure Apache to server the static content directly. From within your VirtualHost stanza, add something like this:

    Alias /static/  /usr/local/static/MyProject/
    

    NOTE: replace the path above with the same path you used when you set STATIC_ROOT

    ---- NEXT ----

    In each application folder, anything you want to get added to your STATIC_ROOT collect into a sub-folder called 'static/'. For example, if you had an app called 'controller', you should have a path that looks like 'controller/static'

    Then from the top of your Django project folder, run the 'collectstatic' sub-command of manage.py. We typically create a script and run it thusly:

    #!/usr/bin/sh
    # Scrtipt to collect django static files together.
    #
    # --link    Create a symbolic link to each file instead of copying.
    # --noinput Do NOT prompt the user for input of any kind.
    #
    python manage.py collectstatic -link --noinput
    exit 0
    

    That's it

    You do NOT need to make any entries in urls.py -- Django will never get to see these requests, so there's no point adding routing entries.

    Downside

    The one big downside to the approach outlined above, it defeats using the Django testserver app. We never use this for any of work, and since our end result will always be published to Apache, we find it's better to only develop there. If you rely on 'testserver' -- there are ways around this limitation. But you'll need to dig through the Django docs a bit https://docs.djangoproject.com/en/dev/howto/static-files/

    Good luck

    0 讨论(0)
  • 2020-12-14 14:19

    Add to the beginning of the urls.py

    from django.conf import settings

    0 讨论(0)
  • 2020-12-14 14:20

    Please check if the settings module is imported in urls.py

    0 讨论(0)
提交回复
热议问题