Django project practice (mall): 14. Supplement: Django file storage class url()

Insert picture description here

(According to the content of teacher's live broadcast)

1. Introduction to Django file storage class url() method

Insert picture description here
Insert picture description here
Insert picture description here

  • in conclusion:
    • The function of the file storage class url() method: returns the URL of the file content represented by name.
    • File storage class url() method trigger: content.image.url
      • Although the url method of ImageField is called on the surface. But internally it will call the url() method of the file storage class.
    • Use of file storage class url() method:
      • We can achieve the purpose of rewriting the url() method by customizing the Django file storage class.
      • The custom Django file storage class must provide the url() method.
      • Returns the absolute URL corresponding to the file pointed to by name.

Two, custom Django file storage class

1. Rewrite the url() method of Django file storage class

class FastDFSStorage(Storage):
    """自定义文件存储系统,修改存储的方案"""
    def __init__(self, fdfs_base_url=None):
        """
        构造方法,可以不带参数,也可以携带参数
        :param base_url: Storage的IP
        """
        # if not fdfs_base_url:
        #     self.fdfs_base_url = settings.FDFS_BASE_URL
        # else:
        # 	  self.fdfs_base_url = fdfs_base_url
        self.fdfs_base_url = fdfs_base_url or settings.FDFS_BASE_URL
    def _open(self, name, mode='rb'):
        ......
    def _save(self, name, content):
        ......
    def url(self, name):
        """
        返回name所指文件的绝对URL
        :param name: 要读取文件的引用:group1/M00/00/00/wKhnnlxw_gmAcoWmAAEXU5wmjPs35.jpeg
        :return: http://192.168.103.158:8888/group1/M00/00/00/wKhnnlxw_gmAcoWmAAEXU5wmjPs35.jpeg
        """
        # return 'http://192.168.15.134.158:8888/' + name
        # return 'http://image.meiduo.site:8888/' + name
        return self.fdfs_base_url + name

2. Related configuration parameters

# 指定自定义的Django文件存储类
DEFAULT_FILE_STORAGE = 'lgshop.utils.fastdfs.fdfs_storage.FastDFSStorage'
# FastDFS相关参数
# FDFS_BASE_URL = 'http://192.168.15.134:8888/'
FDFS_BASE_URL = 'http://image.meiduo.site:8888/'

3. Add the domain name to access the picture

• Add the domain name to access Storage in /etc/hosts

#Storage的IP        域名
192.168.15.134    image.meiduo.site

4. Use of file storage class url() method

• Take the picture carousel as an example: content.image.url

<ul class="slide">
    {% for content in contents.index_lbt %}
    <li><a href="{
     
     { content.url }}"><img src="{
     
     { content.image.url }}" alt="{
     
     { content.title }}"></a></li>
    {% endfor %}
</ul>

5. Turn on the virtual machine: open the storage tracker page to display all the pictures

  • Open fastdfs service
    Insert picture description here
  • Visit homepage
    Insert picture description here

Guess you like

Origin blog.csdn.net/laoluobo76/article/details/115180652