Skip to content

Commit

Permalink
add rich text editor in the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
smark-1 authored May 12, 2024
1 parent a2de20b commit 932eb36
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions faq/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django import forms
from django.contrib import admin
from .models import *
from django.conf import settings

from django.utils.html import strip_tags
# Register your models here.

class AnswerHelpfulAdmin(admin.ModelAdmin):
Expand All @@ -16,13 +17,39 @@ class QuestionHelpfulAdmin(admin.ModelAdmin):
search_fields = ['question', "user"]


class AnswerAdminForm(forms.ModelForm):
class Meta:
model = Answer
fields = "__all__"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
if self.instance.is_rich_text:
try:
from tinymce.widgets import TinyMCE
self.fields["answer"].widget = TinyMCE()
except ImportError:
raise ImportError("Please install django-tinymce to use rich text answers")
elif "rich_text_answers" in settings.FAQ_SETTINGS:
self.instance.is_rich_text = True
try:
from tinymce.widgets import TinyMCE
self.fields["answer"].widget = TinyMCE()
except ImportError:
raise ImportError("Please install django-tinymce to use rich text answers")


class AnswerAdmin(admin.ModelAdmin):
list_display = ("answer", "question", "helpful", "not_helpful",'is_rich_text')
list_display = ("answer_", "question", "helpful", "not_helpful",'is_rich_text')
list_filter = ('helpful', "not_helpful",'is_rich_text')
search_fields = ['answer', "question"]
readonly_fields = ('helpful', "not_helpful", 'slug','is_rich_text')


form = AnswerAdminForm
def answer_(self, obj):
if obj.is_rich_text:
return strip_tags(obj.answer)
return obj.answer
class CategoryAdmin(admin.ModelAdmin):
search_fields = ['name', "_description"]
readonly_fields = ("slug",)
Expand Down

0 comments on commit 932eb36

Please sign in to comment.