Remove Django-jfu and automatically change the same file name

Source: Internet
Author: User
Tags install django

To install Django-jfu, see:

Https://pypi.python.org/pypi/django-jfu


Upload a file using Django-jfu. If the file name is the same, the "_" 1.jpg suffix will be automatically added, for example, 2d7f977e6b36ec616b4e31a33d988c13_1.jpg. We will remove this feature to overwrite the original file.


File: Views. py

Def upload (request ):

Instance. Save ()

Basename = OS. Path. basename (instance. file. Path)

File_dict = {

'Name': basename,

'SIZE': file. size,

'Url': settings. media_url + basename,

'Thumbnailurl': settings. media_url + basename,

'Deleteurl': reverse ('jfu _ delete', kwargs = {'pk': instance. PK }),

'Deletetype': 'post ',

}

Return uploadresponse (request, file_dict)


The original Instance. Save () method will automatically call Django. Core. Files. Storage. filesystemstorage. get_available_name () method. We need to rewrite this method.

Add the following to views. py:

From Django. Core. Files. Storage import filesystemstorage

From Django. DB import models


Class overwritestorage (filesystemstorage ):

Def get_available_name (self, name ):

If self. exists (name ):

OS. Remove (OS. Path. Join (settings. media_root, name ))

Return name


Class photo (models. Model ):

File = models. filefield (upload_to = "/data/Media/", storage = overwritestorage ())

The upload_to directory here is consistent with the settings. media_root directory.

Class photo is not written to model separately. in py, because model. when importing views from overwritestorage in Py, an error is reported, which may be views-> model, but modey-> views cannot be correlated.


Now, the modified file name will automatically overwrite the previous file name.


You also need to modify the update_delete function. Otherwise, one file cannot be deleted.

File: Views. py

Def upload_delete (request, PK ):

Success = true

Try:

Instance = photo. Objects. Get (PK = PK)

If OS. Path. isfile (instance. file. Path ):

OS. Unlink (instance. file. Path)

Instance. Delete ()

Failed t photo. doesnotexist:

Success = false

Return jfuresponse (request, success)


As follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/40/wKiom1PlBn2C3L0tAALAu9oMoiI465.jpg "Title =" jfu.png "alt =" wkiom1plbn2c3l0taalau9omoii465.jpg "/>

The file format of .tar.gz can be uploaded and can be modified:

File: photo_upload_form.html

{% Extends 'jfu/upload_form.html '%}

{% Block js_opts %}

Sequentialuploads: True,

Acceptfiletypes:/(\. | \/) (PNG | GIF | jpe? G | tar.gz) $/I

{% Endblock %}


You can modify the thumbnail in front of server.tar.gz:

File: upload_form.html

About 304 rows:

Data-gallery>

Add a judgment. If the image cannot be found, use the default one.

OK, this problem of automatically adding suffix names has caused me a whole day.

This article is from the "whitel" blog, please be sure to keep this source http://fallinlove.blog.51cto.com/382816/1537679

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.