CKEditor Doesn’t Show Up with a Dynamically Generating WTForms Form Field

Using WTForms, Flask, CKEditor

I have a field in my form that has a dynamic number of entries; they can be both added and removed. Setting this up in WTForms was tricky and I utilized the solution discussed at this link. I am not well acquainted with JS so I’m finding it hard to adapt this function to produce a CKEditorField instead of a regular StringField/<textarea>.

The code below works, nothing is “broken” but the RTE doesn’t show up, only a regular textarea. This led me to believe that the problem is in the HTML rather than the Flask files, but I have included both just in case.

What I have tried so far is:

(1) Replaced Entry form’s bullet StringField with a CKEditorField (Forms.py, below)

(2) Adding “class=ckeditor” to the textarea return and manually calling CKEditor’s replace functions (even though the docs say that this is automatically included) (app.js)

(3) Creating and returning CKEditor.config calls to the HTML for each newly generated field (the docs suggest having this for each CKE on a page, and the other places that I use CKE ((the non-dynamically-created ones)) are working with this in place) (app.js)

(4) Replacing the <textarea> return with a call to CKEditor.create (this one is where I believe the answer probably lies but it hasn’t worked and I can’t figure out what else to try) (app.js/webpage.html)

Also just to note, the other form field, plans, is working with the editor successfully.

Forms.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Entry(FlaskForm):
# (1)
# bullet = StringField(validators=[validators.DataRequired()])
bullet = CKEditorField(validators=[validators.DataRequired(), validators.Length(max=1000)])
class MyForm(FlaskForm):
entries = FieldList(FormField(Entry), min_entries=0)
plans = CKEditorField('Plans', [validators.InputRequired(), validators.Length(max=1000)])
</code>
<code>class Entry(FlaskForm): # (1) # bullet = StringField(validators=[validators.DataRequired()]) bullet = CKEditorField(validators=[validators.DataRequired(), validators.Length(max=1000)]) class MyForm(FlaskForm): entries = FieldList(FormField(Entry), min_entries=0) plans = CKEditorField('Plans', [validators.InputRequired(), validators.Length(max=1000)]) </code>
class Entry(FlaskForm):
    # (1)
    # bullet = StringField(validators=[validators.DataRequired()])
    bullet = CKEditorField(validators=[validators.DataRequired(), validators.Length(max=1000)])

class MyForm(FlaskForm):
    entries = FieldList(FormField(Entry), min_entries=0)
    plans = CKEditorField('Plans', [validators.InputRequired(), validators.Length(max=1000)])

app.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(document).ready(function() {
// (2)
// These are redundant but not working
CKEDITOR.replaceClass = 'ckeditor';
CKEDITOR.replaceAll( 'ckeditor' );
var addCount = 1;
$("#addNewField").click(function() {
var newInput = $("#entries");
newInput.append(GetDynamicTextBox("", addCount));
$("#entries").append(newInput);
addCount += 1;
listCKE();
});
// (3)
// CKE Documentation says to include a config instruction
// (in the form of) {{ ckeditor.config(name="misc_risks") }}
// This fn is my attempt at returning one of those for each added entry to the HTML
// This doesn't work, just returns raw text printed on the page
function listCKE() {
var items = ``;
for (var i=0; i<addCount; i++) {
items += `{{ckeditor.config(name="subdir`+i.toString()+`")}}`;
}
console.log(items)
document.getElementById("listcke").innerHTML = items;
}
});
function GetDynamicTextBox(value, addCount) {
return '<div>' + 'Entry&nbsp' + addCount + ':&nbsp' +
// (4)
// Tried replacing <textarea> with ckeditor create func; didn't work
// '{{ckeditor.create(name="subdir' + addCount + '")}}' +
// (2)
'<textarea class="ckeditor" name = "subdir' + addCount + '"type="text" value = "' + value + '"> </textarea> ' +
'<input type="button" value="Remove" class="remove" />' + '</div>' ;
}
$(function () {
$("#addNewField").click(function() {
$("#entries").append(GetDynamicTextBox("", addCount));
});
$("body").on("click", ".remove", function () {
$(this).closest("div").remove();
addCount -= 1;
});
});
</code>
<code>$(document).ready(function() { // (2) // These are redundant but not working CKEDITOR.replaceClass = 'ckeditor'; CKEDITOR.replaceAll( 'ckeditor' ); var addCount = 1; $("#addNewField").click(function() { var newInput = $("#entries"); newInput.append(GetDynamicTextBox("", addCount)); $("#entries").append(newInput); addCount += 1; listCKE(); }); // (3) // CKE Documentation says to include a config instruction // (in the form of) {{ ckeditor.config(name="misc_risks") }} // This fn is my attempt at returning one of those for each added entry to the HTML // This doesn't work, just returns raw text printed on the page function listCKE() { var items = ``; for (var i=0; i<addCount; i++) { items += `{{ckeditor.config(name="subdir`+i.toString()+`")}}`; } console.log(items) document.getElementById("listcke").innerHTML = items; } }); function GetDynamicTextBox(value, addCount) { return '<div>' + 'Entry&nbsp' + addCount + ':&nbsp' + // (4) // Tried replacing <textarea> with ckeditor create func; didn't work // '{{ckeditor.create(name="subdir' + addCount + '")}}' + // (2) '<textarea class="ckeditor" name = "subdir' + addCount + '"type="text" value = "' + value + '"> </textarea> ' + '<input type="button" value="Remove" class="remove" />' + '</div>' ; } $(function () { $("#addNewField").click(function() { $("#entries").append(GetDynamicTextBox("", addCount)); }); $("body").on("click", ".remove", function () { $(this).closest("div").remove(); addCount -= 1; }); }); </code>
$(document).ready(function() {   
    // (2) 
    // These are redundant but not working
    CKEDITOR.replaceClass = 'ckeditor';
    CKEDITOR.replaceAll( 'ckeditor' ); 


    var addCount = 1;            
    $("#addNewField").click(function() {
        var newInput = $("#entries");
        newInput.append(GetDynamicTextBox("", addCount));
        $("#entries").append(newInput);
        addCount += 1;
        listCKE();        
    });

    // (3)
    // CKE Documentation says to include a config instruction
    // (in the form of) {{ ckeditor.config(name="misc_risks") }}
    // This fn is my attempt at returning one of those for each added entry to the HTML
    // This doesn't work, just returns raw text printed on the page
    function listCKE() {
        var items = ``;
        for (var i=0; i<addCount; i++) {
            items += `{{ckeditor.config(name="subdir`+i.toString()+`")}}`;
        }
        console.log(items)
        document.getElementById("listcke").innerHTML = items;
    }
        
});


function GetDynamicTextBox(value, addCount) {
    return '<div>' + 'Entry&nbsp' + addCount + ':&nbsp' +
    // (4)
    // Tried replacing <textarea> with ckeditor create func; didn't work
    // '{{ckeditor.create(name="subdir' + addCount + '")}}' +
    // (2)
    '<textarea class="ckeditor" name = "subdir' + addCount + '"type="text" value = "' + value + '"> </textarea> ' +
    '<input type="button" value="Remove" class="remove" />' + '</div>' ;
}

$(function () {
    $("#addNewField").click(function() {
        $("#entries").append(GetDynamicTextBox("", addCount));                
            
    });
    
    $("body").on("click", ".remove", function () {
        $(this).closest("div").remove();
        addCount -= 1;
    });
}); 

webpage.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><form method="POST" enctype="multipart/form-data">
{{form.csrf_token}}
<button type="button" id="addNewField">Add Entry</button>
{% for subdir in form.subdirs %}
{{ forms.render_field(subdir.name) }}
{% endfor %}
{{ form.entries() }}
{{ form.plans.label }} {{ form.plans() }}
<p><input type="submit" value="Submit"></p>
</form>
{{ ckeditor.load(pkg_type="basic") }}
{{ ckeditor.config(name="plans") }}
<!-- (4) This was used with the listCKE function in app.js. Didn't work, only returned raw text in {{}} to page -->
<!-- <div id="listcke"></div> -->
</code>
<code><form method="POST" enctype="multipart/form-data"> {{form.csrf_token}} <button type="button" id="addNewField">Add Entry</button> {% for subdir in form.subdirs %} {{ forms.render_field(subdir.name) }} {% endfor %} {{ form.entries() }} {{ form.plans.label }} {{ form.plans() }} <p><input type="submit" value="Submit"></p> </form> {{ ckeditor.load(pkg_type="basic") }} {{ ckeditor.config(name="plans") }} <!-- (4) This was used with the listCKE function in app.js. Didn't work, only returned raw text in {{}} to page --> <!-- <div id="listcke"></div> --> </code>
<form method="POST" enctype="multipart/form-data">
    {{form.csrf_token}}

            <button type="button" id="addNewField">Add Entry</button>
            {% for subdir in form.subdirs %}
            {{ forms.render_field(subdir.name) }}
            {% endfor %}
            {{ form.entries() }}

            {{ form.plans.label }} {{ form.plans() }}

    <p><input type="submit" value="Submit"></p>
</form>
{{ ckeditor.load(pkg_type="basic") }}
{{ ckeditor.config(name="plans") }}
<!-- (4) This was used with the listCKE function in app.js. Didn't work, only returned raw text in {{}} to page -->
<!-- <div id="listcke"></div> -->

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật