Search code examples
pythonhtmldjangodjango-admin

How to add HTML content editor in Django admin view?


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:

rich-text HTML content editor

I appreciate any and all help! Thank you!


Solution

  • 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.