I would like to know the best way to replace a standard textarea field with a rich text editor in Django Admin?
There's an add-on Django application to provide TinyMCE support for Django admin forms without having to muck around with admin templates or Django newform internals.
Take a look on this snippet - basic idea is to include custom JS in your admin definitions which will replace standard text areas with rich-text editor.
For jQuery/FCKEditor such JS could look like that:
$(document).ready(function() {
$("textarea").each(function(n, obj) {
fck = new FCKeditor(obj.id) ;
fck.BasePath = "/admin-media/fckeditor/" ;
fck.ReplaceTextarea() ;
});
});
I'd say: define your own ModelAdmin class and overwrite the widget used for particular field, like:
class ArticleAdminModelForm(forms.ModelForm):
description = forms.CharField(widget=widgets.AdminWYMEditor)
class Meta:
model = models.Article
(AdminWYMEditor is a forms.Textarea subclass that adds WYMEditor with configuration specific to Django admin app).
See this blog post by Jannis Leidel to see how this widget can be implemented.
At the date of the post and the answers TinyMCE was quite popular (as it probably remains today).
But after some time ckeditor has appeared and many consider that a better alternative, including many SO users:
Compare TinyMCE and CKeditor for a Wiki
http://www.turnkeylinux.org/blog/tinymce-vs-ckeditor
There is also a 2013 review of WISIWYG editors with Django in Russian:
http://habrahabr.ru/company/htdt/blog/202782/
Currently the most straight forward way to use tinymce in django admin is to use Grappelli.
http://code.google.com/p/django-grappelli/
Grappelli is also a requirement for django-filebrowser so if you want the whole shebang you will gotta need it anyways.
class KindEditor(forms.Textarea):
class Media:
css ={
'all':(settings.STATIC_ROOT + 'editor/themes/default/default.css',)
}
js = (settings.STATIC_ROOT + 'editor/kindeditor-min.js',settings.STATIC_ROOT + 'editor/lang/zh_CN.js',)
def __init__(self):
attrs = {}
attrs['rel'] = 'kind'
super(KindEditor, self).__init__(attrs)
class NewsAdminForm(forms.ModelForm):
pass
class Meta:
model = News
widgets = {
'body':KindEditor()
}
class NewsAdmin(admin.ModelAdmin):
form = NewsAdminForm
admin.site.register(News, NewsAdmin)
Ok, to update a little this post, I would say that the easiest way to implement TinyMCE is to use the django-tinymce app. You must also download the JS files from the TinyMCE page. I got some errors with the django intenationalization, but downloading the laguage packs from the TinyMCE must be enough.
Install this package
pip install django-ckeditor
then run these commands to migrate.
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
finally restart your Django server.
Once you complete the above steps, you can see the rich text editor in your admin panel fields.
Related
I'm trying to extend an already existing Django App. The app is functioning fine as is, but I would like to add blog functionality.
I've installed Wagtail, using the guidelines here (http://docs.wagtail.io/en/latest/getting_started/integrating_into_django.html) To check wagtail is installed, I have navigated to here:
http://myurl/cms
And the wagtail admin panel is displayed. When I navigate to http://myurl/admin I get the default admin control panel for my Django app, so far so good.
Now I am trying to build the blog.
I found this tutorial:
http://wiseodd.github.io/techblog/2015/06/22/developing-wagtail/
which suggests the following as a first step: -
First, we’ll create our generic page class as home page class is
already created by default when we started Wagtail project.
It then displays this code:
# core/models.py
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index
# We’re deriving our GenericPage from Page class, so that our GenericPage also has Page’s field, e.g. title
class GenericPage(Page):
# Let’s create our custom field, named body which is a rich text
body = RichTextField()
# Index the body field, so that it will be searchable
search_fields = Page.search_fields + (index.SearchField(‘body'),) # To show our body field in admin panel, we have to wrap it with FieldPanel and add it to Page’s field panel content_panels = Page.content_panels + [FieldPanel('body', classname=‘full’)]
I could not find which file I was meant to add this into. I searched the system using grep, and found a number of files that had the text string:
from wagtail.wagtailcore.models import Page
I decided the most likely candidate was in the directory:
env/lib/python2.7/site-packages/wagtail/project_template
Within my original app directory. I added the code above to the models.py file residing in the above directory. I then ran
python manage.py makemigrations
But it said no migrations were found. The next step in the tutorial posted above suggests you should now see three different page types available to create in the control panel, but I can not find the option to create any pages.
Can you tell me if I edited the correct file above, or if I should have edited a different file, and also
Why I am not seeing any option to add a new page in the wagtail control panel?
I have consulted with the documentation here (http://docs.wagtail.io/en/latest/getting_started/tutorial.html) and tried following the 'extend the homepage model' section, but couldn't figure out where the home/models.py file is as there is no folder called home in my Django app.
Thanks for any advice
As the final section of the "integrating into Django" docs says:
You’re now ready to add a new app to your Django project (via ./manage.py startapp - remember to add it to INSTALLED_APPS) and set up page models
Running ./manage.py startapp blog will add a blog app to your project, including an empty models.py - this is where you add your page definitions. (The Wagtail docs don't go into detail on this, because it's just following the standard Django workflow, which is hopefully familiar to anyone with an existing Django project to integrate with...)
Tutorials that use wagtail start my_project as a starting point will omit this step, because the starter project comes with a pre-made models.py with a HomePage model. The site-packages/wagtail/project_template directory you found is actually the 'master' copy of the starter project, which gets cloned at the point that you run wagtail start my_project. Since this isn't hooked up to your current project, changing it had no effect.
I use third-party application (django-flatblocks) in my Django project. How can i change the name of the application in admin interface (grappelli)? I need russian name. I already tried to create localization files (.po.mo) and it is work fine in some parts of admin interface, but in breadcrumbs do not. I think grappelli does not use translation in some templates and the best way to resolve the problem is to replace django-flatblocks verbose_name in apps.py "on the fly". Can i do it somehow?
Found this in the official documentation did. The solution is so simple: For application users
To quote the Manual:
If you’re using “Rock ’n’ roll” in a project called anthology, but you
want it to show up as “Jazz Manouche” instead, you can provide your
own configuration:
# anthology/apps.py
from rock_n_roll.apps import RockNRollConfig
class JazzManoucheConfig(RockNRollConfig):
verbose_name = "Jazz Manouche"
# anthology/settings.py
INSTALLED_APPS = [
'anthology.apps.JazzManoucheConfig',
# ...
]
How would I add a rich-text HTML content editor in my Django admin view?
For example, if I want to change the content on my homepage, what python code would I have to input for the HTML to be displayed when I log into the admin portal?
I want to be able to view all of my pages (hopefully even be able to add/delete them), and edit the content directly from the admin view. Similar to something like this:
http://feincms-django-cms.readthedocs.org/en/latest/_images/item_editor_content.png
I appreciate any and all help! Thank you!
The html shown there is there cuz it is in the database. If what you want is a cms, there are several of those for django like django-cms, wagtail, mezzanine among others.
If, on the other hand, you have a model already and want to display it in the admin, you can register it:
from django.contrib import admin
from myproject.myapp.models import Author
class AuthorAdmin(admin.ModelAdmin):
pass
admin.site.register(Author, AuthorAdmin)
In that example, the Author model will show up in admin for edition.
In the last case, where you already have your model registered but you want to have a WYSIWYG editor, check the django-grid for that kind of package and find one suitable, or install one manually.
You have several options in case you want to try the second approach: ckeditor, tinymce, etc.
I'm using DjangoCMS 3.0 with Django 1.6 and django-filer.
I need a wysiwyg editor for some fields of my custom app. Since with the CMS I have installed djangocms-text-ckeditor, I tried to use it also for my model, like this:
from djangocms_text_ckeditor.fields import HTMLField
class Post(models.Model):
description = HTMLField(_('description'), blank=True, null=True)
This solution seems to work, but I can't add images inside it.
1) How can I add images inside CKeditor widget using cmsplugin_filer_image plugin?
2) Is it a good practice to use djangocms-text-ckeditor outside the CMS?
AFAIK, you can't.
Or by switching CKEditor to "HTML mode" and adding your <img> tag with the source hosted externally.
Likewise for all nested plugins (link, image, file, columns, etc.) which need a placeholder to handle the nested structure.
Yes, but not for complex contents, like medias. Then you can use a PlaceholderField but this is only manageable by staff users through the django-cms frontend editing feature.
I want to pygmentize my text field python code in django admin template in a situation.
I have a python code block which is stored as a text field in django model.Here i stand with the situation of highlighting that python code with syntax in django admin change_form.html whenever you try to access that, it would be in proper syntax highlighiting style.
I have gone through some resources.
All are explained to make django template tag or filter with custom pygment template tag also, i got some nice article too.
1.http://od-eon.com/blogs/stefan/integrating-pygments-django/
2.http://djangosnippets.org/snippets/416/
But I am facing problem of using the template tags or filters in django admin change_form.html ?
Or How can we do the syntax highlighting through django model admin if this is the method should need to be passed (http://dpaste.com/hold/1280580/)?
Assuming you don't need ordering on the python code column, then you could simply render it using pygments API in a ModelAdmin method using list_display. Then you wouldn't need to touch django admin templates at all.