Static files

Page images, audio, video, CSS/JS, etc

<img src="/static/images/1.jpg" width="200" height="100">
<link href="/static/css/test.css" rel="stylesheet">
<script src="/static/js/test.js" type="text/javascript">
<audio src="/static/audio/1.mp3" controls="controls">
<video src="xxxx">
Copy the code

When the browser loads these tags, it automatically sends an HTTP request. When the Django service receives a request that starts with “/static/”, it knows that the request is for a static resource

Configuring static Files

#settings.py
STATIC_URL = "/static/"  Requests that start with /static/ are requests for static resources
STATICFILES_DIRS = (os.path.join(BASE_DIR,"static"),"appname/static".)# static in the root directory, or static in the application
Copy the code

More dynamic

Access static resources in templates

{% load static %}

{% static "images/1.jpg" %}

<img src="{% static 'images/2.jpg'%}">pic</a>
Copy the code

practice

Request/static/images / 1. JPG

  1. Configure static files

Directly fetch resources from /static/ without using the view function even if there is a route matching the view function

  1. You can configure a route to go through the view function

In settings.py, you need to change STATIC_URL=”/static/” or use the static resource

Django Templates 2 next: Django applications