flask_admin.form.upload

class FileUploadField(label=None, validators=None, base_path=None, relative_path=None, namegen=None, allowed_extensions=None, permission=438, allow_overwrite=True, **kwargs)[source]

Customizable file-upload field.

Saves file to configured path, handles updates and deletions. Inherits from StringField, resulting filename will be stored as string.

__init__(label=None, validators=None, base_path=None, relative_path=None, namegen=None, allowed_extensions=None, permission=438, allow_overwrite=True, **kwargs)[source]

Constructor.

Parameters:
  • label – Display label
  • validators – Validators
  • base_path – Absolute path to the directory which will store files
  • relative_path – Relative path from the directory. Will be prepended to the file name for uploaded files. Flask-Admin uses urlparse.urljoin to generate resulting filename, so make sure you have trailing slash.
  • namegen

    Function that will generate filename from the model and uploaded file object. Please note, that model is “dirty” model object, before it was committed to database.

    For example:

    import os.path as op
    
    def prefix_name(obj, file_data):
        parts = op.splitext(file_data.filename)
        return secure_filename('file-%s%s' % parts)
    
    class MyForm(BaseForm):
        upload = FileUploadField('File', namegen=prefix_name)
    
  • allowed_extensions – List of allowed extensions. If not provided, will allow any file.
  • allow_overwrite – Whether to overwrite existing files in upload directory. Defaults to True.

New in version 1.1.1: The allow_overwrite parameter was added.

class ImageUploadField(label=None, validators=None, base_path=None, relative_path=None, namegen=None, allowed_extensions=None, max_size=None, thumbgen=None, thumbnail_size=None, permission=438, url_relative_path=None, endpoint='static', **kwargs)[source]

Image upload field.

Does image validation, thumbnail generation, updating and deleting images.

Requires PIL (or Pillow) to be installed.

__init__(label=None, validators=None, base_path=None, relative_path=None, namegen=None, allowed_extensions=None, max_size=None, thumbgen=None, thumbnail_size=None, permission=438, url_relative_path=None, endpoint='static', **kwargs)[source]

Constructor.

Parameters:
  • label – Display label
  • validators – Validators
  • base_path – Absolute path to the directory which will store files
  • relative_path – Relative path from the directory. Will be prepended to the file name for uploaded files. Flask-Admin uses urlparse.urljoin to generate resulting filename, so make sure you have trailing slash.
  • namegen

    Function that will generate filename from the model and uploaded file object. Please note, that model is “dirty” model object, before it was committed to database.

    For example:

    import os.path as op
    
    def prefix_name(obj, file_data):
        parts = op.splitext(file_data.filename)
        return secure_filename('file-%s%s' % parts)
    
    class MyForm(BaseForm):
        upload = FileUploadField('File', namegen=prefix_name)
    
  • allowed_extensions – List of allowed extensions. If not provided, then gif, jpg, jpeg, png and tiff will be allowed.
  • max_size

    Tuple of (width, height, force) or None. If provided, Flask-Admin will resize image to the desired size.

    Width and height is in pixels. If force is set to True, will try to fit image into dimensions and keep aspect ratio, otherwise will just resize to target size.

  • thumbgen

    Thumbnail filename generation function. All thumbnails will be saved as JPEG files, so there’s no need to keep original file extension.

    For example:

    import os.path as op
    
    def thumb_name(filename):
        name, _ = op.splitext(filename)
        return secure_filename('%s-thumb.jpg' % name)
    
    class MyForm(BaseForm):
        upload = ImageUploadField('File', thumbgen=thumb_name)
    
  • thumbnail_size

    Tuple or (width, height, force) values. If not provided, thumbnail won’t be created.

    Width and height is in pixels. If force is set to True, will try to fit image into dimensions and keep aspect ratio, otherwise will just resize to target size.

  • url_relative_path

    Relative path from the root of the static directory URL. Only gets used when generating preview image URLs.

    For example, your model might store just file names (relative_path set to None), but base_path is pointing to subdirectory.

  • endpoint – Static endpoint for images. Used by widget to display previews. Defaults to ‘static’.
class FileUploadInput[source]

Renders a file input chooser field.

You can customize empty_template and data_template members to customize look and feel.

class ImageUploadInput[source]

Renders a image input chooser field.

You can customize empty_template and data_template members to customize look and feel.