django1.3 New added a app,django.contrib.staticfiles for static resource management. In previous versions of Django, the management of static resources has always been a problem. Some apps are released with a static resource file, and you must manually copy the static resource files from each app to the same static directory when you deploy them. After introducing Staticfiles, you only need to execute./manage.py collectstatic It is easy to copy the static resources used in the app to the same directory.
The introduction of Staticfiles facilitates the management of Django static files, but it feels like Staticfiles's documentation is not too clear, and it makes me confused when I first use it.
The following is a brief introduction to the main configuration of Staticfiles:
- Static_root: The directory to which the static files will be copied after running manage.py collectstatic. Note: Do not put static files of your project into this directory. This directory is only used when running collectstatic. I took it for granted that this directory is the same as the media_root, resulting in the inability to find static files in the development environment.
- Static_url: The starting URL of the static file set, which can only be referenced within the template. This parameter has the same meaning as Media_url.
- Staticfiles_dirs: There are static file locations that need to be managed in addition to the static directory for each app, such as the project's common file-statically files. And the meaning of template_dirs is similar.
- The static files in the static/directory under each app are automatically found in the Django Development server, which is similar to the Templates directory under the previous app.
- Adding code for static file processing in urls.py
From django.contrib.staticfiles.urls import staticfiles_urlpatterns # ... the rest of your URLconf goes here ... Urlpatterns + = Staticfiles_urlpatterns ()