Django Admin: Add TinyMCE to particular TextField only? - django

I have set up TinyMCE to work with the Admin panel (as per the instructions in the Django Docs http://code.djangoproject.com/wiki/AddWYSIWYGEditor )
The problem is that I have Inlines and other text areas within my model for which I don't want TinyMCE to render
Does anyone know how to set TinyMCE to only load for particular fields within my model?
Thanks
EDIT
Ok, so I've installed django-tinymce and configured it
I have created the following in the admin.py of the model with the field I want to add tinymce to:
class FooAdminForm(forms.ModelForm):
class Meta:
model = Foo
def __init__(self, *args, **kwards):
self.bar = forms.TextField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
super(FooAdminForm, self).__init__(*args, **kwargs)
Unfortunately this still isn't working

Right, if anyone is looking to do this:
First make sure the tinymce settings are correct:
TINYMCE_JS_ROOT = '/media/tiny_mce/'
TINYMCE_JS_URL = os.path.join(MEDIA_URL, "tiny_mce/tiny_mce_src.js")
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
}
TINYMCE_SPELLCHECKER = True
Then in the admins.py of your model
from django.forms import *
from django.db.models import *
from tinymce.widgets import TinyMCE
class ProductionForm(forms.ModelForm):
some_field = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
class Meta:
model = Production
class ProductionAdmin(admin.ModelAdmin):
form = ProductionForm

That wiki page is about five years old (!) and these days there's a much easier way of integrating TinyMCE, by simply using the django-tinymce project.
However, since you've already done it this way, you can achieve what you want with a simple change to the textareas.js script. The method described at your link uses mode: textareas, which as you note converts all textareas automatically. What you want is this:
mode: "exact",
element: "id_mytextarea",
where "id_mytextarea" is the HTML ID of the field you do want to convert - usually the name of the model field prefixed by "id_". See the TinyMCE documentation.

Related

Django-Admin and Django-Summernote create a file field in my forms

I am customizing django-admin with summernote and so far so good, but for some reason it creates a file field in my forms which is never allowed to be empty and I can't update records without uploading dummy files. Please see attachment.
My admin.py code is:
rom django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin
from .models import Post, Category, Tag
# Register your models here.
# admin.site.register(Post)
# POST
#admin.register(Post)
class PostAdmin(SummernoteModelAdmin):
""" Registers the model Post in the Admin Site """
list_display = ('title','publish','status') # Columns to display
list_filter = ('status','created','publish','author') # Filter by on right column
search_fields = ('title','body') # Search in these fields
prepopulated_fields = {'slug': ('title',)} # Prepopulate
filter_horizontal = ('tag',)
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
summernote_fields = ('body',)
How can I remove the "file" field from there?
Please help.
Many thanks.
I had this same issue today after upgrading Django-Summernote from version 0.8.11.4 to 0.8.11.5 (the current version).
Can solve two ways:
Comment out Django-Summernote -- don't use
Downgrade to version 0.8.11.4 with pip
pip uninstall django-summernote
pip install django-summernote==0.8.11.4

Is there a way to inject a featured image field in mezzanine / django RichTextPage model?

Is there a way to inject a featured image field in Mezzanine / Django RichTextPage's model? I need get a image, to specific pages in "richtextpage.html" template, but these are different from blogPost models that have a featured image field.
[SOLVED] The problem: I was trying to register/unregister a new field in Page model instead RichTextPage, changing the references got the expected result;
# settings.py
EXTRA_MODEL_FIELDS = (
(
"mezzanine.pages.models.Page.page_image",
"ImageField",
("Featured Image",),
{"blank": True, "upload_to": "uploads", },
), ...
# application admin.py
from copy import deepcopy
from mezzanine.pages.admin import PageAdmin
from mezzanine.pages.models import RichTextPage
(...)
pages_fieldsets = deepcopy(PageAdmin.fieldsets)
pages_fieldsets[0][1]["fields"].insert(-2, "page_image")
pages_fieldsets[0][1]["fields"].insert(-3, "content")
class CustomPageAdmin(PageAdmin):
fieldsets = pages_fieldsets
admin.site.unregister(RichTextPage)
admin.site.register(RichTextPage, CustomPageAdmin)

Tinymce works fine in my app but not in admin - Django

I have a form in my app that has content attribute and uses tinymce like so:
from tinymce.models import HTMLField
class SomeModel(models.Model):
content = HTMLField('Content', null=True)
I also set up my tinymce in the installed apps and have the registered its url.
In my app it works fine and the user can edit and enter his\her content using tinymce. but when I register this model to the admin, all fields appear fine except the content which does not appear to have any way to input (and because its a required field, this means that I cant enter new items through the admin).
this looks like this in the admin:
how can I make tinymce also available in the admin screen?
bonus question: is there a safe way to use tinymce while letting the users use tinymce (currently im using form | safe which im guessing isn't really safe.
Create ModelForm
class BlogForm(ModelForm):
body = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
Admin.py
from models import Blog
from forms import BlogForm
class BlogAdmin(ModelAdmin):
form = BlogForm()

How can I use WYSIWYG editor in Wagtail CMS?

I'm trying to integrate Wagtail CMS with my existing Django project. Other than this basic installation, I made a file named wagtail_hooks.py. Everything's good so far, but I need to use WYSIWYG editor on Wagtail CMS. Is there a way to access models.py for Wagtail so that I can use third-party WYSIWYG editor on model level?
MY_APP/wagtail_hooks.py
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Store
class StoreAdmin(ModelAdmin):
model = Store
menu_label = 'Store' # ditch this to use verbose_name_plural from model
menu_icon = 'doc-full' # change as required
menu_order = 10 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ['id', 'status', 'typ', 'businessName',]
search_fields = ('businessName', 'created_by__username',)
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(StoreAdmin)
Wagtail comes with an excellent WYSIWYG editor, Draftail, based on DraftJS. It is highly extensible:
http://docs.wagtail.io/en/v2.4/advanced_topics/customisation/extending_draftail.html
To use it, you can change your model to use wagtail.core.fields.RichTextField instead of TextField.
There are several other WYSIWYG editors available, for example, you can still use the old editor with this setting:
WAGTAILADMIN_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
}
}
Good luck!

Hiding a model field in Django admin 1.9

I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`