Skip to main content

Preserve files in Django forms during form validation errors. Drop-in replacement for django-file-resubmit

Project description

bw-file-resubmit

bw-file-resubmit is a drop-in replacement for django-file-resubmit, maintained by the BookWyrm project.

In a Django project you may have forms using FileField or ImageField. Everything works great, but when a ValidationError is raised, or you want to add some other check before finally submitting the form, you have to reselect all files and images again. This is because HTML <input type="file"> fields cannot be prepopulated with data due to basic browser security.

bw-file-resubmit solves this problem by caching the file and adding it again in the background. It works with FileField, ImageField and sorl.thumbnail.ImageField (see sorl-thumbnail).

How does it work?

Special widgets can be used to replace FileField and ImageField. When you submit files, every widget saves its file to the cache. When a ValidationError is raised, the widgets restore files from cache so the user doesn't have to upload them again.

  • ResubmitFileWidget can be used in place of ClearableFileInput
  • ResubmitImageWidget can be used in place of ClearableFileInput
  • AdminResubmitImageWidget can be used in place of AdminFileWidget (Django) or AdminImageWidget (sorl-thumbnail)
  • AdminResubmitMixin will apply the appropriate widget for all FileFields and ImageFields

Installation

pip install bw-file-resubmit

Configuration

Add "file_resubmit" to INSTALLED_APPS:

INSTALLED_APPS = {
    ...
    'file_resubmit',
    ...
}

Setup cache in settings.py:

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

Examples

models.py

from django.db import models
from django.db.models import ImageField
# or if you use sorl-thumbnail:
from sorl.thumbnail.fields import ImageField

class Page(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    image =  ImageField(upload_to='whatever1')

Form widgets

Ordinary form

forms.py

from django.forms import ModelForm
from file_resubmit.widgets import ResubmitImageWidget, ResubmitFileWidget
from .models import Page

class PageModelForm(forms.ModelForm)

    class Meta:
        model = MyModel
        widgets = {
            'picture': ResubmitImageWidget,
            'file': ResubmitFileWidget,
        }

class Book(forms.Form):
    form = PageModelForm

Admin form

admin.py

NOTE: admin.AdminResubmitFileWidget has been dropped from bw-file-resubmit. If you need to update a legacy django-file-resubmit project, use widgets.ResubmitFileWidget instead.

from django.forms import ModelForm
from file_resubmit.admin import AdminResubmitImageWidget,
from file_resubmit.widgets import ResubmitFileWidget
from .models import Page

class PageModelForm(forms.ModelForm)

    class Meta:
        model = MyModel
        widgets = {
            'picture': AdminResubmitImageWidget,
            'file': ResubmitFileWidget,
        }

class PageAdmin(admin.ModelAdmin):
    form = PageModelForm

admin.site.register(Page, PageAdmin)

Use as a model mixin in admin

admin.py

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

class PageAdmin(AdminResubmitMixin, admin.ModelAdmin):
    pass

admin.site.register(Page, PageAdmin)

Licensing

bw-file-resubmit is free software under terms of the MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bw-file-resubmit-0.6.0rc2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

bw_file_resubmit-0.6.0rc2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file bw-file-resubmit-0.6.0rc2.tar.gz.

File metadata

  • Download URL: bw-file-resubmit-0.6.0rc2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for bw-file-resubmit-0.6.0rc2.tar.gz
Algorithm Hash digest
SHA256 c1665c44c45309808838ab6068840f22ac59fbc4f8f5c655b015e6d1b253f84c
MD5 6e3ebe0bef0523bf80fb43fb234d906b
BLAKE2b-256 477de26848abc7d762897c33d2d39c6c1db60806b6e75fe8e73227f9ee394d0e

See more details on using hashes here.

File details

Details for the file bw_file_resubmit-0.6.0rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for bw_file_resubmit-0.6.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 60689d2d8f66ae53346649e48f5a4c1a90834631042f7ea95f90493b69fbf25f
MD5 3e45ca1d5540bb40fe0c086edbbf2fce
BLAKE2b-256 b7730beb70000c9381f29b773a5f936c77b665b8a3b9d7e68e9efbdd5432004c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page