Skip to content
Simple photo gallery for Django websites
Branch: master
Clone or download
aaugustin Update migration to match 6f8adeb.
This has no effect on the database, so it's safe to alter an existing
migration rather than create a new migration.
Latest commit ea52944 Aug 18, 2018


mYk's gallery


myks-gallery is a simple photo gallery with granular access control.

It powers my humble photo gallery, allowing me to:

  • access my entire photo collection privately,
  • share some albums with family or friends,
  • make some albums public.

Use case

Rather than use a photo manager, I just create a new directory for each event and put my photos inside. I include the date of the event in the name of the directory and I rename photos based on their date and time. Then I regularly synchronize my collection to a remote storage. I serve my gallery from there.

If you have a similar workflow, you may find myks-gallery useful.

Whenever I upload new photos, I re-scan the collection with ./ scanphotos or with the button in the admin. myks-gallery detects new albums and photos. Then I define users, groups and access policies in the admin.

Album access policies control the visibility of albums. Most often, you'll enable the "photos inherit album access policy" option. If you need more control, for instance to share only a subset of an album, you can disable this option and use photo access policies. You still need to define an album access policy and it should be a superset of the photo access policies.

Obviously, requiring usernames and passwords doesn't work well for sharing photos with relatives. You might want to use django-sesame.


myks-gallery is a pluggable Django application. It requires Django 1.11 (LTS), 2.0 or 2.1. It also depends on Pillow. It works with any version of Python supported by Django.


myks-gallery requires two storage areas:

  • The first one contains the original photos. It's a read-only reference. You can upload photos there with aws s3 sync, gsutil rsync, rsync, etc. depending on the platform. (I don't know any such tool for Azure storage.)
  • The second one contains downscaled photos and ZIP archives of album exports. It's a read-write cache. You can set up expiry policies and clear it without affecting the gallery, aside from the cost of rescaling images again.

myks-gallery accesses them through Django's file storage API, meaning that you can use any storage for which a Django storage backend exists. You should use a third-party storage backend if you're storing files in a cloud service and Djang's built-in FileSystemStorage if you're storing them locally on the filesystem, typically for local development.

Installation guide

This application isn't exactly plug'n'play. There are many moving pieces. Here's the general process for integrating myks-gallery into an existing website:

  1. Download and install the package from PyPI:

    $ pip install myks-gallery
  2. Add gallery.apps.GalleryConfig to INSTALLED_APPS:

    INSTALLED_APPS += 'gallery.apps.GalleryConfig',
  3. Configure the settings — see below for the list.

  4. Add the application to your URLconf with the gallery application namespace:

    urlpatterns += [
        path('gallery/', include('gallery.urls', namespace='gallery')),
  5. Create a suitable base.html template. It must provide three blocks: title, extrahead, content, as shown in this example.

  6. Optionally, if you're serving files from the local filesystem, enable X-accel (nginx) or mod_xsendfile (Apache) for your photo and cache directories.

  7. Scan your photos with the "Scan photos" button in the admin or the scanphotos management command and define access policies.

The source contains a sample application in the example directory. It can help you see how everything fits together. See below for how to run it.


myks-gallery defines two permissions:

  • "Can scan the photos directory" allows using the "Scan photos" button in the admin.
  • "Can see all photos" allows seeing all albums and all photos regardless of access policies.



Default: not defined

Dotted Python path to the Django storage class for the original photos. It must be readable by the application server but should not be writable.

While GALLERY_PHOTO_STORAGE behaves like Django's DEFAULT_FILE_STORAGE setting, you'll usullay point it to a factory function that initializes and returns a Django storage instance because you won't want to use globally configured values for settings such as the S3 bucket name.

For compatibility for versions prior to 0.5, if GALLERY_PHOTO_STORAGE isn't defined but GALLERY_PHOTO_DIR is, the photo storage will be set to FileSystemStorage(location=GALLERY_PHOTO_DIR).


Default: not defined

Dotted Python path to the Django storage class for the thumbnails and album archives. It must be readable and writable by the application server.


For compatibility for versions prior to 0.5, if GALLERY_CACHE_STORAGE isn't defined but GALLERY_CACHE_DIR is, the photo storage will be set to FileSystemStorage(location=GALLERY_CACHE_DIR).


Default: ()

Tuple of (category name, regular expression) defining how to extract album information — category, date, name — from the paths of photo files.

The regular expressions match paths relative to the root of the photo storage. They contain the following captures:

  • a_name: album name (mandatory) — to capture several bits, use a_name1, a_name2, etc.
  • a_year, a_month, a_day: album date (mandatory)
  • p_year, p_month, p_day, p_hour, p_minute, p_second: photo date and time (optional)

Here's an example, for photos stored with names such as 2013/01_19_Snow in Paris/2013-01-19_19-12-43.jpg:



Default: ()

Tuple of regular expressions matching paths within GALLERY_PHOTO_STORAGE. Files matching one of these expressions will be ignored when scanning photos.


Default: {}

Dictionary mapping thumbnail presets names to (width, height, crop). If crop is True, the photo will be cropped and the thumbnail will have exactly the requested size. If crop is False, the thumbnaill will be smaller than the requested size in one dimension to preserve the photo's aspect ratio.

The default templates assume the following values:

    'thumb': (128, 128, True),
    'standard': (768, 768, False),

You may double these sizes for better results on high DPI displays.


Default: {}

Dictionary mapping image formats names to to dictionaries of options for Pillow's save method. Options are described for each file format in Pillow's documentation.

The following a reasonable value for high-quality thumbnails and previews:

    'JPEG': {'quality': 95, 'optimize': True},


Default: ''

Name of the HTTP header that triggers sendfile on your web server. Use 'X-Accel-Redirect' for nginx and 'X-SendFile' for Apache.


Default: ''

Part to strip at the beginning of the paths in the sendfile header. The header will contain the absolute path to files, minus this prefix. This is generally useful for nginx and not necessary for Apache.


Default: "Gallery"

Title of your photo gallery. This is only used by the default templates of the index and year views.


Default: 5

Number of thumbnails shown in the preview of each album.


Default: None or 60

Duration in days during which album archives are kept in cache. None disables expiration.

When using a remote storage system such as S3, configuring an expiry policy for the export folder directly on the storage system is more efficient.

For compatibility with versions prior to 0.5, if GALLERY_CACHE_DIR is defined, GALLERY_ARCHIVE_EXPIRY defaults to 60.

Running the sample application

  1. Make sure Django and Pillow are installed

  2. Download some pictures (warning: these files are large, total = 50MB; you can use photos of your own instead as long as you respect the format of the directory name: YYYY_MM_DD_album name):

    $ cd example
    $ mkdir cache
    $ mkdir photos
    $ mkdir "photos/2013_01_01_Featured Pictures"
    $ cd "photos/2013_01_01_Featured Pictures"
    $ wget
    $ wget
    $ wget   JPG
    $ wget
    $ wget
    $ wget
    $ wget
    $ wget
    $ wget
    $ wget
    $ cd ../..
  3. Run the development server:

    $ ./ migrate
    $ ./ runserver
  4. Go to http://localhost:8000/admin/gallery/album/ and log in. Click the "Scan photos" link at the top right, and the "Scan photos" button on the next page. You should see the following messages:

    • Scanning path/to/myks-gallery/example/photos
    • Adding album 2013_01_01_Featured Pictures (Photos) as Featured Pictures
    • Done (0.01s)

    Go to http://localhost:8000/ and enjoy!

    Since you're logged in as an admin user, you can view albums and photos even though you haven't defined any access policies yet.



Under development


  • Updated for Django 2.0.


  • Added migrations for compatibility with Django 1.9.

To upgrade an existing project, run: django-admin migrate --fake-initial.


This version uses the Django file storage API for all operations on files, making it possible to use services such as Amazon S3 or Google Cloud Storage for storing photos and thumbnails. It introduces the GALLERY_PHOTO_STORAGE and GALLERY_CACHE_STORAGE settings. They supersede GALLERY_PHOTO_DIR and GALLERY_CACHE_DIR.

When upgrading to 0.5 or later, you should clear the cache directory. Previously cached thumbnails and exports won't be used by this version.

It also include some smaller changes.

  • Switched ordering of albums to always show the most recent albums first.
  • Allowed customizing the number of photos in album previews.
  • Preserved original order of photos in album previews.
  • Added pagination on album preview pages.
  • Changed the hashing schema. This invalides the cache. You should clear it.
  • Fixed collision between zip archives containing photos with the same name.


  • Provided exports of albums as zip archives.
  • Fixed preview of photos affected by batch access policy changes.


  • Support for Python 3 and Django 1.6.
  • Hid public albums by default for logged-in users.
  • Switched the default styles to a responsive design.
  • Added an option to scanphotos to precompute thumbnails.
  • Added an option to scanphotos to resynchronize photo dates.
  • Fixed bugs in photo dates.


  • Made most settings optional for easier deployment.
  • Made "sendfile" optional and used streaming responses as a fallback.
  • Worked around a crash in libjpeg when creating large JPEG previews.
  • Added many tests.


  • Initial public release, with the history from my private repository.
  • Added documentation (README file).
  • Added a sample application.
You can’t perform that action at this time.