Skip to main content

Django widget for multiple image input

Project description

Django MultipleImageInput

Widget for multiple image fields. Works good with PostgreSQL array fields.

Example

In this example i use crispy_forms

settings.py

import pathlib
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MEDIA_URL = '/media/'
MEDIA_ROOT = pathlib.Path(BASE_DIR) / 'common' / 'media'

model.py

from django.contrib.postgres.fields import ArrayField
from django.db import models


class Product(models.Model):
    class Meta:
        db_table = 'product'
        verbose_name = 'товар'
        verbose_name_plural = 'товары'

    IMAGE_PATH = 'common/image/product'

    title = models.TextField('заголовок', null=True, blank=True)
    description = models.TextField('описание', null=True, blank=True)
    price = models.IntegerField('цена')
    youtube_url = models.URLField('видео youtube', null=True, blank=True)
    image_list = ArrayField(models.TextField("картинки"), default=list, verbose_name='картинки', blank=True)

view.py

from settings import MEDIA_ROOT
from models import Product
import django_multipleimageinput

class UploadImageView(django_multipleimageinput.UploadImageView):
    media_root = MEDIA_ROOT
    image_path = Product.IMAGE_PATH

forms.py

from django import forms
from models import Product
from django_multipleimageinput import MultipleImageInput
from django.urls import reverse_lazy
from crispy_forms.helper import FormHelper
from crispy_forms.layout import ButtonHolder, Submit, Layout


class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ["title", "description", "price", "youtube_url", "image_list"]
        widgets = {
            "title": forms.TextInput(),
            "description": forms.Textarea(attrs={'rows': 4, 'cols': 15}),
            "image_list": MultipleImageInput(reverse_lazy('common:product/upload-image'), MEDIA_URL)
        }

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()

        self.helper.layout = Layout(
            'title',
            'description',
            'price',
            'youtube_url',
            'image_list',
            ButtonHolder(
                Submit('submit', 'Сохранить')
            )
        )
        super(ProductForm, self).__init__(*args, **kwargs)

urls.py

from django.urls import path
from django.conf.urls.static import static as sttc
import views
import settings

app_name = 'common'

urlpatterns = [
    path('product/upload-image', views.UploadImageView.as_view(), name='product/upload-image'),
] + sttc(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

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

django_multipleimageinput-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_multipleimageinput-0.0.1.tar.gz.

File metadata

  • Download URL: django_multipleimageinput-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.7.5

File hashes

Hashes for django_multipleimageinput-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3cac528faf782ce84fc69e69ed832eb05ea3c36fd6deb4999fa2b9e85d422ee2
MD5 fb353cfe42151888ace83956ca13f16d
BLAKE2b-256 d901f396cccd19551ff7eec727666eb32b9f7eea95d73c5a5a48041fca894284

See more details on using hashes here.

File details

Details for the file django_multipleimageinput-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_multipleimageinput-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.7.5

File hashes

Hashes for django_multipleimageinput-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aeaf5b28aa793e13499f678d005ecfc62be06240a5b800df75737ce580a5f306
MD5 f6c53ac86f6c5d601628ee92dbad86e8
BLAKE2b-256 a90856d02a1d12c73eeea4fb560f855d9cf6b4ffe25140c49b1e1cb0f920a7f0

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