staticfiles and STATIC_URL with virrtualenv - djan

2019-07-08 01:43发布

I'm using virtualenv and I can't get staticfiles to work on my django,

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/home/brian/Projects/RaffleFish/static',

)

My virtualenv directory is virt_env/virtFish

How do I get my staticfiles to work? do I add the directory structure of virtenv

1条回答
爷、活的狠高调
2楼-- · 2019-07-08 02:15

Inside the individual applications within your Django project you can create a static directory and place static files within that directory. Those files will automatically be handled by the django.contrib.staticfiles module which should be in the INSTALLED_APPS tuple in your settings.py file. These files will be aggregated and served during the development process automatically by the python manage.py runserver command.

The trick to this process that you must keep in mind is that when it comes time to deploy to production, or any environment where you aren't serving content with manage.py you will have to perform that aggregation yourself. The manage.py command has a command to help with this. As a part of your deploy you should include the command:

python manage.py collectstatic

This will collect (aggregate) all the static files from the static directories in your apps into a single location defined in settings.py by the STATIC_ROOT setting.

查看更多
登录 后发表回答