views:

472

answers:

1

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.

+7  A: 

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

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 ;)

Ludwik Trammer
I was actually looking at it - but my image field is separate from the content field... reason being I want to control how the article image is displayed as I might not always need it - do you have any custom code to control how the article is displayed? ( Other than say, styling image elements in a certain way with CSS within articles ). And what about resizing and controlling the dimensions of article images?
meder
I'm sorry, but I'm not sure I understand the question... What exactly do you want to be able to do?
Ludwik Trammer
The image depicts embedding the image source in a certain format inside of the article content itself. Let's say I had an RSS Feed and I was generating text, I obviously wouldn't want the image in there, and this doesn't just apply to say RSS, but if I had a search index and I had a thumbnail in my article I wouldn't want it to show up either - with django-adminfiles can you target not just textareas but `ImageField`s? Or perhaps I need to convert my `ImageField` to a textfield or a tiny textarea so it can be separate from the content.
meder
I updated my original answer. I think this addresses the issue. Basically django-adminfiles is very flexible. You can have different template for every situation (and also for different file types). In a template you can define how the image is displayed... or that it shouldn't be displayed at all.
Ludwik Trammer
I should probably also mention that there exists a django-filebrowser-no-grappelli project at http://github.com/jbeaurain/django-filebrowser-no-grappelli/. But django-adminfiles is just SO much exciting ;)
Ludwik Trammer
By the way - what editor do you use, if any? Im using MarkItUp w/ MarkDown but it doesn't show up when I try to preview it or after I save it -I suspect I have to odify the templates.
meder
As for your edit: Have you followed the "Usage" instructions at http://bitbucket.org/carljm/django-adminfiles/src/?Specifically you haven't mentioned coping files from `adminfiles/media/adminfiles` to `MEDIA_URL/adminfiles`, and adding `url(r'^adminfiles/', include('adminfiles.urls')` to your project's urls.py.I also use markItUp!, but with textile. I don't use the in-admin-preview feature, but the formatting works great on the website itself. Effectively my templates look something like this: `{{ article.content|textile|render_uploads }}`
Ludwik Trammer
Yeah, I followed all 4 steps including copying the media files, the media files are there for sure because they generate the image picking area, I added the url to urls.py. Maybe I'll try Textile?
meder
Oh, I thought django-adminfiles isn't working for you at all. Anywhere you want to see the image rendered you need to use "render_uploads" filter - that includes MarkItUp preview. Your choice of markup language doesn't make any difference.
Ludwik Trammer