c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Python

How to integrate NicEdit rich text editor for the description field in a Django Python?

| | django , python

To integrate NicEdit in django Admin:

Download NicEdit from official website or register NicEdit script into project. Add NicEdit JavaScript file into project's static folder. Create an folder named "js" and place the NicEdit JavaScript file.

Django Admin Configuration:

form.py:

from .models import Product
from django import forms
class ProductForm(forms.ModelForm):
    class Meta:
        model = Product  # Specify your model
        fields = '__all__'  # Or list the fields you want to include
    # Add a widget to the description field
    description = forms.CharField(widget=forms.Textarea(attrs={'class': 'nic-edit','cols': 80}))


admin.py:

from django.contrib import admin
from .models import *
from .form import ProductForm
class ProductAdmin(admin.ModelAdmin):
  list_display = ('name', 'description')
  list_per_page=10
  search_fields=['name','category__name']
  form = ProductForm
  class Media:
        js = ('/static/js/nicEdit.js', '/static/js/script.js')
admin.site.register(Product, ProductAdmin)
script.js:
document.addEventListener("DOMContentLoaded", function() {
    var textAreas = document.getElementsByClassName("nic-edit");
    for (var i = 0; i < textAreas.length; i++) {
        new nicEditor({ fullPanel: true }).panelInstance(textAreas[i]);
    }
});
 rich text editor for description field in django admin


VIDEO GUIDE: