For Django, static files are generally handled by the Web server, and Django itself does not handle static files. To enable the Django development environment to handle static files, Django has a static file configuration that differs from the production environment.
Django Version: 1.10
Development (DEV) environment configuration
OneStatic_url, Staticfiles_dirs, Static_root
1. Create the static directory under the app directory, and place the files and folders in this directory, such as Your_app/static/img
2. Ensure that the Installed_apps in the settings.py contains Django.contrib.staticfiles, and that debug is True
3. Set the value of Static_url in settings.py to "/static/", action reference 5
4. At the beginning of the template {%load static%} After that, the {{Static_url}} is used as the static file path prefix in the template. such as CSS file your_app/static/css/Bootstrap.min.css, then the application code is<link href="{% static " css/bootstrap.min.css "%}" rel="stylesheet">
5. You can test the static files via URL alone (HTTP://127.0.0.1:8000/STATIC/CSS/BOOTSTRAP.MIN.CSS)
6. Staticfiles_dirs is used to configure the address of some additional static files, as configured below. When accessing the template, refer to 4.
Staticfiles_dirs = [
Os.path.join (Base_dir, "static"),
'/var/www/static/',
]
7. Static_root, the absolute path is set, the static files are copied to this directory when the collectstatic is running, and it is more useful to transplant static files from the development environment to the production process.
Ii. Media_root and Media_url
Media_root: Refers to the files uploaded by the user, such as Filefield,imagefield uploaded in the model. If you define media_root= '/users/xxx/media/', then file=models. Filefield (upload_to= "App01/pic"), the uploaded file will be saved to '/users/xxx/media/app01/pic '
Media_url:url mapping, before and after adding '/' means starting from the root directory, such as "/site_media/", plus this property, the static file link is preceded by this value.
1. Set media_url in the settings file, such as Media_url= '/media/'
2. Set media_root in the settings file, can be anywhere (not the same as Static_root), to save the uploaded file
3. The URLs file is configured as follows,
from Import Settings from Import = [ # ... the rest of your URLconf goes here ...] + static (settings. Media_url, Document_root=settings. Media_root)
4. In the settings file, add ' Django.template.context_processors.media ' under 'context_processors' in templates
After configuration, you can use the browser to access the uploaded files, http://ip:port/MEDIA_URL/upload_path/file. For example Media_url set is '/media/', media_root set is '/user/xxx/', in the models set the file upload (upload_to=) ' App01/pic ', then the actual storage address after file upload is/ User/xxx/app01/pic. Accessed via URL: http://ip:port/media/app01/pic/test.jpg. Access this uploaded image in the front-end Template: <img src="{{ media_url}}app01/pic/test.jpg" ">
See the official documentation for more information.
Production process Deployment
The biggest difference in the deployment phase is that you have to have the Web server handle the static files, so you have to configure them in the Web server so that the Web server can access the static files. Official documentation reference here. Later, you will add your own notes after the test.
Django Static configuration file