STATICFILES_DIRS = (os.path.join(BASE_DIR, ‘static’),)
STATICFILES_DIRS
=(
os.path.join(BASE_DIR, ‘static’),)
Can be created according to the address of the static resource You can also create your own Match addresses one by one
for example : /static/images/logos/ lenovo .png
with open(file_name, ‘wb’) as f:
'wb'
w write in b Binary form wb Write... In binary format
# Upload brand logo
class UploadBrandLogo(APIView):
def post(self, request):
print(' Uploaded image data :', request.data, type(request.data))
# Get file object
file = request.data.get('file')
# file.file Uploaded file byte stream object
# file.name File name
# file_path File path
# Splicing path
static_path = 'static/images/logos'
file_path = os.path.join(settings.BASE_DIR, static_path)
file_name = os.path.join(file_path, file.name)
# Save the picture
with open(file_name, 'wb') as f:
f.write(file.file.read())
data = {
'static_path': static_path,
'code': 200, 'msg': ' Upload the picture successfully '
}
return Response(data)
Be careful : When brand routing uses view sets The route of brand image uploading can be written into the main route to avoid repetition
urlpatterns = [
path('brands/logos/', UploadBrandLogo.as_view()),
]