Using ACE in Django Admin

2017-03-31 19:52 (7 years ago) ytyng

1. Add to requirements.txt

django-ace

2. pip install

pip install -r requirements.txt
(Alternatively, $ pip install django-ace)

3. Add to INSTALLED_APPS in settings

    'django_ace',

4. Modify ModelAdmin

from django_ace import AceWidget
from django.db import models as django_models

...
    formfield_overrides = {
        django_models.TextField: {'widget': AceWidget(
            mode="django", width="100%", height="500px")},
    }

Alternatively, you can create a separate form to set different modes for different text fields

from django import forms as django_forms
class TeikeiTypeAdminForm(django_forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(* args, **kwargs)
        self.fields['note'].widget = AceWidget(
            mode="markdown", width="100%", height="500px")
        self.fields['js_function'].widget = AceWidget(
            mode="javascript", width="100%", height="500px")

class TeikeiTypeAdmin(admin.ModelAdmin):
    form = TeikeiTypeAdminForm
list_display = (
...

*Highly recommended if you're using Mezzanine, as it works very well.

Current rating: 5

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011