First, upload
1.models.py
Create a picture of the field, and set the upload path:
image = models.ImageField(upload_to='imgstype', verbose_name='图片')
2.settings.py
Modify settings:
MEDIA_URL = '/imgs/' #显示图片时通过该路径索引图片
MEDIA_ROOT = os.path.join(BASE_DIR, 'imgs').replace("//", "/")#设置上传图片的保存位置
The above setting shows:
upload pictures to '/ imgs / imgstype /' under the project folder
Second, display pictures
1. Set the route, making it possible to index pictures
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)```
2.template template
<img src="{{ category.image.url }}">