【问题标题】:Dynamic upload_to not working properly动态 upload_to 无法正常工作
【发布时间】:2018-12-17 01:56:11
【问题描述】:

我正在尝试使用 python 遍历我的硬盘驱动器并将文件存储在数据库中。我的动态upload_to 在上传文件时正在工作,但是当我尝试从我编写的算法中保存文件以循环通过硬盘驱动器时,它不起作用(upload_to)。请问有什么帮助吗?

我将添加以下代码:

models.py

def upload_path(instance, filename):
    return os.path.join('file', instance.folder, filename)

类文件(models.Model):

#Defintion to set upload_to path


file_title = models.CharField(max_length=255)
file_type = models.CharField(max_length=255)
folder = models.CharField(max_length=255)
document = models.FileField(upload_to=upload_path)
uploaded_at = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return self.file_title

def delete(self):
    delete_path = os.path.join('file', self.document.path)

    os.remove(delete_path)
    return super(File,self).delete()

views.py (sn-p) - 遍历硬盘并保存到数据库的算法

for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'file'):
                    #Work with file info
                    x = File()
                    x.file_title = filename
                    x.file_type = filetype
                    x.folder = folder
                    x.document = sfile

                    x.save()

循环算法:

def SyncDriveToDatabase(request):

HttpResponse(request, 'brick/loading.html')

#############################
#File Folder
#############################

primary_directory = os.path.join(root, 'file/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)


for cfile in os.listdir(primary_directory):
    if os.path.isfile(primary_directory + cfile):


        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'file'):
            #Work with file info
            x = File()
            x.file_title = filename
            x.file_type = filetype
            x.folder = folder
            x.document = cfile

            x.save()



    elif os.path.isdir(primary_directory + cfile):
        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'file'):
                    #Work with file info
                    x = File()
                    x.file_title = filename
                    x.file_type = filetype
                    x.folder = folder
                    x.document = sfile

                    x.save()

##################################
#Audio Folder
##################################

primary_directory = os.path.join(root, 'audio/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)

#Loop through primary_directory
for cfile in os.listdir(primary_directory):

    if os.path.isfile(primary_directory + cfile):

        #Extract File information
        filename, filetype = os.path.splitext(cfile)
        album = 'Unkown'
        artist = 'Unkown'

        if not exists(filename, 'audio'):
            a = Audio()
            a.audio_title = filename
            a.audio_type = filetype
            a.album = album
            a.artist = artist
            a.document = cfile
            a.save()



    elif os.path.isdir(primary_directory + cfile):
        artist_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(artist_directory):

            if os.path.isfile(sfile):

                #Extract File information
                filename, filetype = os.path.splitext(sfile)
                album = 'Unkown'
                artist = cfile
                if not exists(filename, 'audio'):
                    #Work with file info
                    a = Audio()
                    a.audio_title = filename
                    a.audio_type = filetype
                    a.album = album
                    a.artist = artist
                    a.document = sfile
                    a.save()

            elif os.path.isdir(artist_directory + sfile):
                album_directory = os.path.join(artist_directory, sfile) + '/'

                for tfile in os.listdir(album_directory):

                    if os.path.isfile(album_directory + tfile):

                        #Extract File information
                        filename, filetype = os.path.splitext(tfile)
                        album = sfile
                        artist = cfile

                        if not exists(filename, 'audio'):
                            #Work with file info
                            a = Audio()
                            a.audio_title = filename
                            a.audio_type = filetype
                            a.album = album
                            a.artist = artist
                            a.document = tfile
                            a.save()







######################
#Image Folder
#######################

primary_directory = os.path.join(root, 'image/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)


for cfile in os.listdir(primary_directory):
    if os.path.isfile(primary_directory + cfile):

        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'image'):
            #Work with file info
            i = Image()
            i.image_title = filename
            i.folder = folder
            i.document = cfile
            i.save()

    elif os.path.isdir(primary_directory + cfile):

        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile
                if not exists(filename, 'image'):
                    #Work with file info
                    i = Image()
                    i.image_title = filename
                    i.folder = folder
                    i.document = sfile
                    i.save()

######################
#Video Folder
#######################

primary_directory = os.path.join(root, 'video/')

if not os.path.exists(primary_directory):
    os.makedirs(primary_directory)

for cfile in os.listdir(primary_directory):

    if os.path.isfile(primary_directory + cfile):

        #Extract File Information
        filename, filetype = os.path.splitext(cfile)
        folder = 'Main'

        if not exists(filename, 'video'):
            #Work with file info
            v = Video()
            v.folder = folder
            v.video_title = filename
            v.document = cfile
            v.save()

    elif os.path.isdir(primary_directory + cfile):

        secondary_directory = os.path.join(primary_directory, cfile) + '/'

        for sfile in os.listdir(secondary_directory):
            if os.path.isfile(secondary_directory + sfile):

                #Extract File Information
                filename, filetype = os.path.splitext(sfile)
                folder = cfile

                if not exists(filename, 'video'):
                    #Work with file info
                    v = Video()
                    v.folder = folder
                    v.video_title = filename
                    v.document = sfile
                    v.save()





return redirect('index')

【问题讨论】:

  • 文件夹中的cfile在哪里= cfile
  • 我不明白什么不起作用。您似乎没有上传任何内容。
  • cfile 是一个存储在变量cfile 中的文件。我没有上传任何东西,是的,我只是想将数据保存到模型中。从数据库中检索文件。
  • 我已经添加了整个循环算法,以便您了解cfile是什么
  • 另外,当我只是将数据保存到硬盘驱动器(不从网站上传,从算法保存)时,它没有正确地将 upload_to 保存在 FileField 中,它将它保存为 /media/文件名而不是 /media/folder/filename

标签: python django django-models django-views django-file-upload


【解决方案1】:

我使用 Daniel 的意见解决了我的问题。我更改了文档的值并且它起作用了。请参阅下面的新代码:

旧代码:

...
x.document = sfile

x.save()

新代码:

x.document = os.path.join('file', folder, sfile)

x.save()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多