write sth. upfront
In fact, media configuration can also be completely replaced by static (depending on your own choice), static instead of the method is directly in the settings of the user to upload avatars, change the user to upload avatars when the location of the save
When set to static/avatar/, we can add a static in front of the user uploaded file to get it (equivalent to getting static file resources!!!).
configure
In django, the static file resources we need are placed in the static folder by default
Files uploaded by users should also be placed in a separate folder
The role of the media configuration: This configuration allows users to upload all files to be stored in a specified folder.
Later, when we access the files uploaded by the corresponding users from the database, we can also take out the corresponding
1.1 Configuring the storage location for user uploaded files
Write the following code in: MEDIA_ROOT = (BASE_DIR,'media') # User uploaded files are placed in the media folder of the project file # No need to create the folder yourself,After a user uploads a file,The system will automatically create it for you
1.2 Open back-end designated resources
Write the following specified code in the
# Expose specified folder resources on the backend re_path(r'^media/(?P<path>.*)',serve,{'document_root':settings.MEDIA_ROOT}) # The above fixed write, media is and static a meaning, url meet media will automatically go to the back of the settings.MEDIA_ROOT configured path to find the corresponding resource
2. User avatar display
# {{ article_obj. }} is to get where the document is stored at that time # Add media to mean that when you meet media, you go to the media folder and look for files uploaded by the user. <img class="media-object" src="/media/{{ article_obj. }}" alt="..." width="60">
to this article on media configuration and the user's avatar from the database to the front end of the article is introduced to this, more related media configuration content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!