Django admin multiple photo upload - 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.

Related

Django admin edit only field?

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 a django admin widget that allows the admin to sort model objects by fields values?

I am building an app in Django.
I found there is a very easy way to integrate a widget into django admin that allows the admin to filter model objects by fields values. That is achieved by including the line
list_filter = ['field_to_filter_by_its_values']
into the class mymodelAdmin(ImportExportModelAdmin) in admin.py, as shown below
class target_area_history_dataAdmin(ImportExportModelAdmin):
resource_class = target_area_history_dataResource
list_filter = ['Target_area_input_data__Name']
admin.site.register(target_area_history_data, target_area_history_dataAdmin)
Now, instead of integrate a widget to filter my model objects by that field, is there a way to integrate a widget to sort my model objects by that field?
Note: I am using Django Import-Export in my model.
I'll suggest you use django-treebeard. This allows you to view tree nodes hierarchically in the administration interface, with interface features dependent upon the tree algorithm used.
# admin.py
from django.contrib import admin
from treebeard.admin import TreeAdmin
from .models import Category
class CategoryAdmin(TreeAdmin):
list_display = ("title", "created", "modified",)
list_filter = ("created",)
admin.site.register(Category, CategoryAdmin)
What's cool about this is that you can not only sort (by clicking the header row) but also drag things around, as shown in this image.
I recommend you using the grapelli admin interface that allows what you need and a bit more. here you have the grapelli project page and the https://github.com/sehmaschine/django-grappelli.
It's a well documented package and is plug and play for what you need. It also gives a fresh face to Django Admin and is compatible with Django import/export package.

Differences between Stacked inline and Tabular inline

This is my models.py file
from django.db import models
# Create your models here.
class Item(models.Model):
name=models.CharField(max_length=250)
description = model.TextField()
class Meta:
oredering['name']
def __unicode__(self):
return self.name
#permalink
def get_absolute_url:
retun ('item_detail',None,{'object_id':self_id})
class Photo(models.Model):
item = models.ForiegnKey(Item)
title=models.ChaField(max_length=250)
image=models.IMageField(upload_to='photos')
caption=models.TextField(blank=True)
class Meta:
ordering=['title']
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
retun ('photo_detail',None,{'object_id':self_id})
And this is my admin.py :
from django.contrib import admin
from models import Item
from models import Photo
# Register your models here.
class PhotoInline(admin.StackedInline):
model = Photo
class ItemAdmin(admin.ModelAdmin):
inlines = [PhotoInline]
admin.site.register(Item, ItemAdmin)
admin.site.register(Photo)
But, I can't understand what is StackedInline and TabularInline, I referred to Django documentation but still couldn't understand what exactly it was.
Also, I can't see those models in my admin panel when I started the server, I don't understand why my models aren't registered on my admin page.
I see two different questions:
I cant understand what is stacked inline and tabular inline
Basically, both allow you to edit models on the same page as a parent model. In other words, it is sometimes preferable for a user to have the possibility to edit a certain model while editing another one instead of having to manually add another instance somewhere else in your interface. In your particular case, you can use it to facilitate user experience by allowing the user to add photos linked to a parent item simultaneously whitout having to constantly change between your admin forms.
Now, the difference between the two is really simple to understand: Layout. Indeed, both works exactly the same behind the scenes, the only difference is the template used for rendering. It can be seen here in the source code. So, picking one for your project is only a matter of preference regarding the interface layout
I cant see those models in my admin panel
This can be a lot of things, but often it's because you forgot to run your migrations with makemigrations and migrate. If you did, another thing that a lot of users forget is to install their app. So, in
Setting.py
INSTALLED_APPS = ['Myproject.apps.Myapp']
The TabularInline displays data in table
But StackedInline displays in row

How to upload multiple images in a Django field

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.

Django: How to edit a gallery that is attached to a project model?

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..