un1t / django-file-resubmit

It remembers selected files in form on validation errors.
76 stars 39 forks source link

FileField not working for django-file-resubmit #22

Open AmeyKelekar opened 6 years ago

AmeyKelekar commented 6 years ago

Hi,

I am referring https://github.com/un1t/django-file-resubmit that will help me not to reselect file on a Validation Error. But somehow I am not able to implement that functionality using 'file_resubmit'

I will share the steps followed: 1) pip install django-file-resubmit

2) In settings. py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ...
    'file_resubmit',
]

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    },
    "file_resubmit": {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        "LOCATION": '/tmp/file_resubmit/'
    },
}

3) models.py has following line for file upload:

document_copy_of_CDA = models.FileField(upload_to='documents/%Y/%m/%d', validators=[validate_file_extension])

4) admin.py has following lines:

from django.contrib import admin
from file_resubmit.admin import AdminResubmitMixin
from .models import Consulting

class ConsultingAdmin(AdminResubmitMixin, admin.ModelAdmin):
    pass

admin.site.register(Consulting, ConsultingAdmin)

5) forms.py has following lines:

from file_resubmit.admin import AdminResubmitImageWidget, AdminResubmitFileWidget

class ConsultingForm(forms.ModelForm):
    class Meta:
    model = Consulting
    exclude = ('user', 'status')
    widgets = {
        'picture': AdminResubmitImageWidget,
        'file': AdminResubmitFileWidget, 
    }

Can you please check and let me know if I have to do any kind of modifications or addition to my exisiting code to take advantage of resubmit functionality? Thanks in advance. Looking forward to hearing from you.

Regards, Amey Kelekar

luyidong commented 6 years ago

Me too , with Django 1.10.6

forms.py has following lines:

        widgets = {
            'file': AdminResubmitFileWidget,
          }

resubmit to detect web content:

 name="file_cache_key"   

field already exists ,but it also prompts need to reselect the file

...

How to fix it?

maddencs commented 6 years ago

Can you explain what exactly is happening? I've created a test project in Django 1.10.6 that mimics @AmeyKelekar 's post, but I don't have any problem uploading files via admin. While I do see the text that says "No File Selected"(I'll look into trying to remove that), it still retains the file for me.

luyidong commented 6 years ago

I know why form https://github.com/un1t/django-file-resubmit/issues/2

@un1t  said:
Hi! You can't set value to file input in browsers due to security reasons. 

Therefore widget renders filename near a file input. If you have any ideas how to fix it let me know.

--

I need to customize page content to avoid duplicate input.