I have checked many uploaders for django image field. But I am unable to get a simple clear way of doing multiple image uploads in Django.
My requirement is
Class Foo(models.Model):
images = SomeImageField(upload_to = "/path/")
This should allow me to upload multiple images. Now django-photologue allows Gallery upload, but this is only in zip format. I want something similar. Is there any such app available ?
django-filer will allow you to upload multiple images via a separate interface (not via a model field, but via the django admin), but you will only be able to select one of those uploaded image per image field. What you need to do is implement a django admin StackedInline or something similar
# models.py
from django.db import models
from filer.fields.imagefields import FilerImageField
class MyObject(models.Model):
name = models.CharField(...)
class Image(models.Model):
image_file = FilerImageField()
obj = models.ForeignKey(MyObject, ...)
# admin.py
from django.contrib import admin
from models import Image, MyObject
class ImageInline(admin.StackedInline):
model = Image
class MyObjectAdmin(admin.ModelAdmin):
inlines = [ImageInline, ]
...
now you will be able to easily attach multiple images to a single instance of your object via the admin.
I don't know of any apps that allow for a single field to manage multiple images.
Related
I'm using the Froala Django Editor in some forms in my Django REST Framework backend, such as this
# resources/forms.py
from django import forms
from froala_editor.widgets import FroalaEditor
from .models import Resource
class ResourceForm(forms.ModelForm):
class Meta:
model = Resource
fields = '__all__'
widgets = {
'content': FroalaEditor(
options={
'heightMin': 256
}
)
}
When I try to upload an image (or a video, or any file, but one thing at a time) in the Froala editor I get an error:
In the console I have:
GET https://{some-id}.cloudfront.net/uploads/froala_editor/images/Nights.of.Cabiria.jpg [HTTP/2 403 Forbidden 15ms]
The error above made me wonder that perhaps the image is being uploaded correctly, but the Froala editor can't get it after uploading in order to display it.
The application is being hosted in AWS and the uploaded files stored in S3 buckets.
And in fact, I checked in the S3 dashboard, and the images are there, so they have uploaded correctly.
Even though I'm using all default FROALA_EDITOR_OPTIONS. I'm aware there are specific options for S3 storage (I've tried them) but I'm not using them and it is uploading fine.
Still looking at that error, I remembered that in other models in the project I have ImageFields, for example
# users/models.py
class User(AbstractUser):
name = models.CharField(_('First name'), db_index=True, max_length=255)
image = models.ImageField(upload_to=user_image_bucket, verbose_name=_('Image'))
def signed_image_url(self):
try:
return sign_cloudfront_url(self.image.url)
except ValueError:
return None
and that the serializers for these models always return the signed url, not the original url of the image
# users/serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'id',
'name',
'signed_image_url',
)
I don't understand much about AWS S3, but I suppose that the images stored there are not publicly accessible, and providing a signed url to an image grants access to it.
Knowing this, I believe that what I need to do is to apply the same sign_cloudfront_url function to the images uploaded via the Froala Editor.
According to the Froala docs, you can set some events listeners on the Froala Editor options, such as image.uploaded, and I think this is where I should get the url of the uploaded image and return a signed url, but I'm not being able to set these events.
If I do this:
# resources/forms.py
from django import forms
from froala_editor.widgets import FroalaEditor
from .models import Resource
class ResourceForm(forms.ModelForm):
class Meta:
model = Resource
fields = '__all__'
def uploaded():
print("hello world")
widgets = {
'content': FroalaEditor(
options={
'heightMin': 256,
'events': {
'image.uploaded': uploaded,
}
}
)
}
I get a very expected Object of type function is not JSON serializable error.
Any idea how I should handle this?
I have a model that I only want to use one row of its table. So, on admin, I would like to remove the list and add pages, and only edit the existing object. The model is this:
from django.db import models
class Banner(models.Model):
pass
class BannerImg(models.Model):
img = models.ImageField(upload_to="banners")
banner = models.ForeignKey(Banner, on_delete=models.CASCADE)
Basically, the Banner(pk=1) will be loaded by the frontend to display a landing page hero slider. I want multiple images, but also want them to be on the same admin form, so one could order, add or remove images from the same place. Of course having to Banner objects, wouldn't make sense in this case.
I can use inline fields to do the form, but how can I achieve the pages functionality (going directly to edit)?
Thanks!
Accordion to documentation.
from django.contrib import admin
class BannerImgInline(admin.TabularInline):
model = BannerImg
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImgInline,
]
Is there anyway where we can build logic Using django rest framework
where user can add blog with multiple images and content accordingly and when saved
and retrieved it should be able to display the same kind of UI depening up on the frontend app
same like medium platform
Note:
My question isn't about adding multiple images and content using Rest framework
but its about fetching and displaying the data based on how user sent it the server
For eg:
<Image>
content for that image
<Image2>
content for this image
i just want to know how to associate those images to that content
i want to add content to that image
or is there anyway where we can store image and all the content exacty and save it in TextField
I've searched a lot about this but unfortunately I've not found a way to make this happen
Read about relationships in Django (and SQL in general)
django relations
it sounds like you're looking for something like the following:
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
# Always override the user model provided by Django when starting a project. the docs themselves state that.
pass
class Image(models.Model):
image = models.ImageField()
added = models.DateTimeField(auto_now_add=True)
# using get_user_model to get the User model, always better then referencing User directly
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name="user_images",
null=False,
blank=False
)
class ImageContent(models.Model):
title = models.CharField(max_length=140, null=False, blank=False)
content = models.TextField(max_length=500)
image = models.OneToOneField(Image, on_delete=models.CASCADE, null=False, blank=False)
Some notes:
I haven't dealt myself with Images field But I remember it does require a special library (pillow).
getting the data in a certain order should be easy enough if you understand the queryset object:
queryset link
using stuff like order_by will help you return the response in the order you like.
the models I've written here are not the only way to achieve the goal you've set, I strongly recommend reading about relations and models in Django.
I have to create a blog posting system and I need to make it possible to upload multiple photos in Django Admin and select it via Django TinyMCE. One of the solutions is Filebrowser, but I have already spent a few days and haven't got it worked. Are there any alternatives?
I'm trying to do something similar, but not with Tinymce, I choose wmd.
I created two models, one for the blog post, and one for the images, and in the admin interface I included the images as inlines. here's some example.
in your model.py file:
class Project(models.Model):
...
#TinyMce field.
description = models.TextField()
class ProjectImage(models.Model):
image = models.ImageField(upload_to='prjimages/%Y/%m/%d/%H/%M/%S/')
project = models.ForeignKey(Project)
than in your admin.py file, you can have the PostImage as inline.
from django.contrib import admin
from models import *
class ProjectImageAdmin(admin.ModelAdmin):
pass
class ProjectImageInline(admin.StackedInline):
model = ProjectImage
max_num=10
extra=0
class ProjectAdmin(admin.ModelAdmin):
inlines = [ProjectImageInline,]
admin.site.register(ProjectImage, ProjectImageAdmin)
admin.site.register(Project, ProjectAdmin)
you can change the ImageField Widget to show the url, and maybe a preview for each image (I have no example code for this at the moment). And if the user want to include the image in the post, he can just copy paste the url to Tinymce.
It's not a complete solution, but maybe you can extend it with some work to fit your needs.
You should take look at Dropzone.js which you can easily drag and drop multiple images to admin page.
I have a 'project' model. Each project has a 'gallery' and each gallery has 'photos'.
class Project:
gallery = ForeignKey(Gallery)
class Gallery:
photos = ManyToManyField(Photo)
class Photo:
image = ImageField(...)
I want to let my users edit the gallery and the project on the same page. Could you tell me what components I need to make this happen? Like which type of form I should use and what technique to use when I process the form with the uploaded images and all?
What to take into account is that I want to show the photos the user is editing with the html img-tag as well as file-tag to let him replace the photo. I don't want django's default m2m-widget which is just a multiselect-list.
Could you help me figure this out, because I simply can't. Been stuck here for three days :)
You can modify your Gallery Admin form using Project as a admin.TabularInline.
Like this:
admin.py
# -*- encoding: utf-8 -*-
from models import Project, Gallery, Photo
from django.contrib import admin
class ProjectInline(admin.TabularInline)
model = Project
class GalleryAdmin(admin.ModelAdmin):
inlines = [ProjectInline]
admin.site.register(Gallery, GalleryAdmin)
I didn't want to use the built in admin module. But I used the formset factory by django. It will let me provide a queryset to the formset (i.e the photos in the gallery). Then I had to provide a small customized model formset class, and then i the view I pretty much had to process the form manually in order to link it correct to the gallery and such..