Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Dojo Rich Editor Implementation in Admin.py

77 views
Skip to first unread message

Gchorn

unread,
May 1, 2012, 1:34:31 AM5/1/12
to django...@googlegroups.com
Hi All,

I'm building a blog, with models "Post" and "Image" (each blog post can have multiple images; they're related by ForeignKey) and I'm trying to implement the Dojo rich editor in my admin site by following the example here:

https://gist.github.com/868595

However, the rich editor is not showing up.  I have a feeling I don't have something set up correctly in my admin.py file; could anyone tell me what it is?  Here are the contents of admin.py:

from blogs.models import Post,Image
from django.contrib import admin
from django.contrib.admin import site, ModelAdmin
import models


class CommonMedia:
  js = (
    'https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js',
    '/home/guillaume/mysite/blogs/static/editor.js',
  )
  css = {
    'all': ('/home/guillaume/mysite/blogs/static/editor.css',),
  }

class PostImageInline(admin.TabularInline):
    model = Image
    extra = 5

class PostAdmin(admin.ModelAdmin):
    inlines = [PostImageInline]

site.register(models.Post,
    list_display = ('text',),
    search_fields = ['text',],
    Media = CommonMedia,
)

admin.site.unregister(Post)

admin.site.register(Post, PostAdmin)

Gchorn

unread,
May 3, 2012, 2:26:40 AM5/3/12
to django...@googlegroups.com
So after doing some more research and playing around with the settings, I want to post an updated version of my question.  Here it is.

I'm building a blog, with models "Post" and "Image" like so:

class Post(models.Model):
    title
= models.CharField(max_length=1000)
    author
= models.CharField(max_length=200)
    pub_date
= models.DateTimeField('date published')
    text
= models.TextField(max_length=10000)

class Image(models.Model):
    post
= models.ForeignKey(Post, related_name='images')
    image
= models.ImageField(upload_to='photos/%Y/%m/%d')
    caption
= models.CharField(max_length=1000,blank=True)

I've been trying to implement the Dojo rich editor in my admin site by following the example here:

http://lazutkin.com/blog/2011/mar/13/using-dojo-rich-editor-djangos-admin/

However, there's some kind of interference between settings in my admin.py file that is keeping me from registering both the rich editor and a ModelAdmin class for uploading images associated with an instance of "Post." If I use the following code in my admin.py file:

from django.contrib.admin import site, ModelAdmin
import models

class CommonMedia:
    js
= (
     
'https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js',

     
'editor.js',
   
)
    css
= {
     
'all': ('editor.css',),
   
}

site
.register(models.Post,
   
Media = CommonMedia,
)

The rich text editor shows up fine. But if I add some code for image uploading fields, like so:

from blogs.models import Post,Image
from django.contrib import admin
from django.contrib.admin import site, ModelAdmin
import models


class CommonMedia:
  js
= (
   
'https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js',

   
'editor.js',
 
)
  css
= {
   
'all': ('editor.css',),

 
}

class PostImageInline(admin.TabularInline):
    model
= Image
    extra
= 5

class PostAdmin(admin.ModelAdmin):
    inlines
= [PostImageInline]

site
.register(models.Post,
    list_display
= ('text',),
    search_fields
= ['text',],
   
Media = CommonMedia,
)

admin
.site.unregister(Post)

admin
.site.register(Post, PostAdmin)

the rich editor doesn't show up. I'm guessing this is due to the "admin.site.unregister(Post)" line, but if I don't use that I get the error, "The model Post is already registered." If instead I remove both that line and the "Post" from "admin.site.register(Post, PostAdmin)", I get the error: "'MediaDefiningClass' object is not iterable".  I know I must be going about registering my ModelAdmin and the rich editor incorrectly, but I can't seem to find any documentation online about "admin.site.register" and "site.register," so I'm totally stuck.  Any help on this would be much appreciated!

cheers,

Guillaume

Reply all
Reply to author
Forward
0 new messages