szaman
szaman

Reputation: 6756

change django-tinymce content css for various object

How can I change content_css option in django-tinymce for various objects? I mean that when I use tinymce for model1 then content_css is model1.css and when for model2 then model2.css. I use

Upvotes: 2

Views: 1362

Answers (2)

lucidyan
lucidyan

Reputation: 3893

Thanks for the szaman's reply, I'll try to describe the process for beginners in new versions of Django (1.9), how to add custom css files to the field named 'text' in the Post model in the Django's admin

  1. Change fields' type in models.py to HTMLField

    from tinymce.models import HTMLField
    
    class Post(models.Model):
        title = models.TextField(default='')
        subtitle = models.TextField(default='')
        text = HTMLField(default='')
    
  2. In the application with the required model, add to admin.py file:

    @admin.register(Post)  # decorator for adding Django admin for Post
    class PostAdmin(admin.ModelAdmin):
        form = PostForm  # attach custom form
    
  3. Add form's class

    from tinymce.widgets import TinyMCE
    
    class PostForm(forms.ModelForm):
    
        class Meta:
            model = Post
            fields = '__all__' # required in Django (>=1.8)
            widgets = {
                'text': TinyMCE(mce_attrs={'content_css': ["path_to_css_file",]}),
            }
    

Upvotes: 3

szaman
szaman

Reputation: 6756

I found out that I can pass extra arguments for tiny_mce in Meta class:

class Meta:
        model = MyModel
        widgets = {
            'field_name': TinyMCE(mce_attrs={'content_css': "style.css"}),
        }

Upvotes: 5

Related Questions