django 1.5 static files 404 error

4999 단어
인터넷에서 찾아보니 공식적인 해결 방법이 있다

Configuring static files


Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'


In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>


Store your static files in a folder called static in your app. For example my_app/static/my_app/myimage.jpg.
Now, if you use ./manage.py runserver, all static files should be served automatically at the STATIC_URL and be shown correctly.
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. For example:
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
)

See the documentation for the STATICFILES_FINDERS setting for details on how staticfiles finds your files.
코드 섹션:
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

BASE_DIR = os.getcwd()

# 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.

    os.path.join(BASE_DIR, "static"),
)

좋은 웹페이지 즐겨찾기