1. The process of uploading and displaying pictures after Django integrates FastDFS
2.FastDFS Installation (Operation in Virtual Machine Environment), (omitted)
3.Django Project Core Code
(1) File location:
(2)storage_util.py
from django.core.files.storage import Storage from django.conf import settings from fdfs_client.client import Fdfs_client class FDFSStorage(Storage): '''fast dfs File Storage Class''' def __init__(self): '''Initialization''' self.client_conf=settings.FDFS_CLIENT_CONF self.base_url=settings.FDFS_URL def _open(self,name, mode='rb'): '''Use when opening a file''' pass def _save(self,name, content): '''Use when saving files''' #Name: You choose the name of the uploaded file #Content: File object that contains the content of your uploaded file #Create a Fdfs_client object client=Fdfs_client(self.client_conf) #Upload files to fast dfs system res=client.upload_by_buffer(content.read()) print('res',res) # # { # 'Uploaded size': '9.69KB', # 'Storage IP': b'192.168.12.189', # 'Group name': b'group1', # 'Status': 'Upload successed.', # 'Local file name': '', # 'Remote file_id': b'group1/M00/00/00/wKgMvVvHKzeAFiJkAAAmv27pX4k7691647' # } if res.get('Status')!='Upload successed.': #Upload failure raise Exception('Upload files to FastDFS fail') #Get the returned file ID filename=res.get('Remote file_id').decode() return filename def exists(self, name): '''Django Determine whether the file name is available''' return False def url(self, name): '''Return the access file url Route''' return self.base_url+name
(3) Configuration added to settings.py
#FastDFS Settings - Classes for Custom Storage DEFAULT_FILE_STORAGE='utils.fdfs.storage_util.FDFSStorage' #FastDFS Settings - Client Configuration File FDFS_CLIENT_CONF='utils/fdfs/client.conf' #FastDFS Settings - url FDFS_URL='http://192.168.12.189:9999/'
(4)client.conf
Client.conf needs to add a modified configuration, which can be opened by the sudo vim/etc/fdfs/client.conf command (following is my configuration, which may vary depending on the environment).
# connect timeout in seconds # default value is 30s connect_timeout=30 # network timeout in seconds # default value is 31s network_timeout=60 # the base path to store log files base_path=/home/cuixin/fastdfs/tracker # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.12.189:22122 #standard log level as syslog, case insensitive, value list: ### emerg for emergency ### alert ### crit for critical ### error ### warn for warning ### notice ### info ### debug log_level=info # if use connection pool # default value is false # since V4.05 use_connection_pool = false # connections whose the idle time exceeds this time will be closed # unit: second # default value is 3600 # since V4.05 connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server # since V4.05 # default value is false load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # default value is false # since V4.05 use_storage_id = false # specify storage ids filename, can use relative or absolute path # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # since V4.05 storage_ids_filename = storage_ids.conf #HTTP settings http.tracker_server_port=80 #use "#include" directive to include HTTP other settiongs ##include http.conf
(5)urls.py
from django.conf.urls import url from . import views urlpatterns = [ #url(r'^$',views.index,name='index'), url(r'^$',views.IndexView.as_view(),name='index'), ]
(6)views.py
from django.shortcuts import render from django.views.generic import View from goods.models import * # def index(request): # return render(request,'index.html') class IndexView(View): def get(self,request): # Get information about the types of goods goodstype_list = GoodsType.objects.all() # Preparing a Data Dictionary context = {'goodstype_list':goodstype_list} #Back to home page return render(request,'test_index.html',context)
(7)test_index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for goodstype in goodstype_list %} <li> <ul> <li>{{ goodstype.name }}</li> <li><img src="{{ goodstype.image.url }}"></li> </ul> </li> {% endfor %} </ul> </body> </html>
4. advantages
FastDFS Solution:
(1) Massive storage of files
(2) Document duplication
Integration with Nnix:
(1) Improving the efficiency of static file access