Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: What's an awesome plugin to maintain images in the admin?

I have an articles entry model and I have an excerpt and description field. If a user wants to post an image then I have a separate ImageField which has the default standard file browser.

I've tried using django-filebrowser but I don't like the fact that it requires django-grappelli nor do I necessarily want a flash upload utility - can anyone recommend a tool where I can manage image uploads, and basically replace the file browse provided by django with an imagepicking browser?

In the future I'd probably want it to handle image resizing and specify default image sizes for certain article types.

Edit: I'm trying out adminfiles now but I'm having issues installing it. I grabbed it and added it to my python path, added it to INSTALLED_APPS, created the databases for it, uploaded an image. I followed the instructions to modify my Model to specify adminfiles_fields and registered but it's not applying in my admin, here's my admin.py for articles:

from django.contrib import admin
from django import forms
from articles.models import Category, Entry
from tinymce.widgets import TinyMCE
from adminfiles.admin import FilePickerAdmin

class EntryForm( forms.ModelForm ):

    class Media:
    js = ['/media/tinymce/tiny_mce.js', '/media/tinymce/load.js']#, '/media/admin/filebrowser/js/TinyMCEAdmin.js']

    class Meta:
        model = Entry

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = { 'slug': ['title'] }


class EntryAdmin( FilePickerAdmin ):
    adminfiles_fields = ('excerpt',)
    prepopulated_fields = { 'slug': ['title'] }
    form = EntryForm

admin.site.register( Category, CategoryAdmin )
admin.site.register( Entry, EntryAdmin )

Here's my Entry model:

class Entry( models.Model ):
    LIVE_STATUS = 1
    DRAFT_STATUS = 2
    HIDDEN_STATUS = 3
    STATUS_CHOICES = (
    ( LIVE_STATUS, 'Live' ),
    ( DRAFT_STATUS, 'Draft' ),
    ( HIDDEN_STATUS, 'Hidden' ),
    )
    status = models.IntegerField( choices=STATUS_CHOICES, default=LIVE_STATUS )
    tags = TagField()
    categories = models.ManyToManyField( Category )
    title = models.CharField( max_length=250 )
    excerpt = models.TextField( blank=True )
    excerpt_html = models.TextField(editable=False, blank=True)
    body_html = models.TextField( editable=False, blank=True )
    article_image = models.ImageField(blank=True, upload_to='upload')
    body = models.TextField()
    enable_comments = models.BooleanField(default=True)
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    slug = models.SlugField(unique_for_date='pub_date')
    author = models.ForeignKey(User)
    featured = models.BooleanField(default=False)

    def save( self, force_insert=False, force_update= False):
    self.body_html = markdown(self.body)
    if self.excerpt:
        self.excerpt_html = markdown( self.excerpt )
    super( Entry, self ).save( force_insert, force_update )

    class Meta:
    ordering = ['-pub_date']
    verbose_name_plural = "Entries"

    def __unicode__(self):
    return self.title

Edit #2: To clarify I did move the media files to my media path and they are indeed rendering the image area, I can upload fine, the <<<image>>> tag is inserted into my editable MarkItUp w/ Markdown area but it isn't rendering in the MarkItUp preview - perhaps I just need to apply the |upload_tags into that preview. I'll try adding it to my template which posts the article as well.

like image 349
meder omuraliev Avatar asked Dec 30 '22 02:12

meder omuraliev


2 Answers

Try django-adminfiles - it's awesome. You can upload images or choose existing ones, and insert them anywhere within the text, where they will be displayed as <<<placeholders>>>. You fully control how the placeholders are resolved on the actual site, using special templates - allowing you to control how images are displayed on the site (that includes scaling, using for example sorl.thumbnail template tag).

The app can easily support inserting files of other types - you just create a templates for a given file type, showing how it should be presented on your website. It even supports inserting from Flicker and YouTube.

As you can see I'm still really stoked about finding it :)

It will look something like this in your admin panel (although the screenshot is from a really old version):

django-adminfiles http://lincolnloop.com/legacy/media/img/django-admin-uploads.png

Update: You have full control over when and how images are displayed. Some examples:

  1. For the main article page I want the default image display style, so that's what I write in my template (this will render the images according to rules I defined in my default adminfiles/render/image/default.html template):

    {{ article.content|render_uploads }}

  2. But let's say in my e-mail newsletter I don't want to embed the image - I just want to link to it. I will write this in the template (this will render the images according to rules I defined in my adminfiles/render_as_link/default.html template):

    {{ post.content|render_uploads:"adminfiles/render_as_link" }}

  3. I don't want to have image embed code (or any other HTML for that matter) in my search index, so in my django-haystack template I just do this to remove all HTML:

    {{ article.content|render_uploads|striptags }}

  4. Or I can do this to index images' captions (but not images themselves; again, this assumes you've got adminfiles/only_caption/default.html template with the right code in place):

    {{ article.content|render_uploads:"adminfiles/only_caption" }}

  5. Ok, but what if I want to preserve all other HTML formatting, but don't want to display the images? I'll create an empty adminfiles/blank/default.html file and to this:

    {{ article.content|render_uploads:"adminfiles/blank" }}

There are some other ways you could achieve the same effect (for example by choosing right ADMINFILES_REF_START and ADMINFILES_REF_END and not using the filter at all), but I think this post is too long already ;)

like image 168
Ludwik Trammer Avatar answered Dec 31 '22 16:12

Ludwik Trammer


Grappelli isn't a requirement to use django-filebrowser. I use this implementation in just about all of my projects: https://github.com/wardi/django-filebrowser-no-grappelli

Happy coding!

like image 28
Brandon Avatar answered Dec 31 '22 14:12

Brandon