简体   繁体   English

Django-ckeditor 不使用脆表单保存编辑

[英]Django-ckeditor not saving edits with crispy-forms

I have a cripsy form and I want to change one field from Textarea to CKEDitorUploadingWdidget我有一个脆弱的表格,我想将一个字段从Textarea更改为CKEDitorUploadingWdidget

So my form looks like this (I have left in what was previoulsy working:所以我的表格看起来像这样(我已经离开了以前的工作:

class RenameStudyForm(BetterModelForm):
    name = forms.CharField(label='Study Name', max_length=51, required=False) # Update study name
    #waiver = forms.CharField(widget=forms.Textarea, label='Waiver of Documentation', required=False) 
    waiver = forms.CharField(widget=CKEditorUploadingWidget(), label='Waiver of Documentation', required=False)

I have amended my model as follows:我修改了我的 model 如下:

class study(models.Model):
    researcher = models.ForeignKey("auth.user") # Researcher's name
    name = models.CharField(max_length = 51) # Study name
    instrument = models.ForeignKey("instrument") # Instrument associated with study
    #waiver = models.TextField(blank = True) 
    waiver = RichTextUploadingField(blank = True)

My template looks has:我的模板看起来有:

    {% load crispy_forms_tags %}
    {{ form.media }}
    {% crispy form %}    

When I enter the screen to edit the waiver I get a rich text field to edit, as I would expect.当我进入屏幕编辑豁免时,我会得到一个富文本字段来编辑,正如我所期望的那样。 However, nothing I enter into the field is passed back to the form.但是,我在该字段中输入的任何内容都不会传递回表单。 Within the form I added a print statement, as below在表格中我添加了一个打印语句,如下所示

def clean(self):
    cleaned_data = super(RenameStudyForm, self).clean()
    print(cleaned_data['waiver'])

The print always gives the original text.印刷品总是给出原始文本。 Can anyone help me please谁能帮帮我

EDIT编辑

I've been reviewing console when I'm using the CKEditorUploadingWidget against the forms.Textarea widget and it appears to be generating the following jQuery warning当我对 forms.Textarea 小部件使用 CKEditorUploadingWidget 时,我一直在查看控制台,它似乎正在生成以下 jQuery 警告

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.不推荐使用主线程上的同步 XMLHttpRequest,因为它对最终用户的体验产生不利影响。

I believe I am getting this because I am loading the form into a modal using this button我相信我得到了这个,因为我正在使用此按钮将表单加载到模式中

<button type="button" class="btn btn-secondary btn-block"   onclick = "modal_form('/interface/study/{{ current_study|urlencode }}/rename_study/')" >Update Study</button>

And this view而这个观点

def rename_study(request, study_name):
    #do stuff
    return render(request, 'researcher_UI/add_study_modal.html', form_package)

So my JavaScript for ckeditor is being loaded now rather than when the document is originally loaded so I think this causes the issues.所以我的 ckeditor 的 JavaScript 现在正在加载,而不是在文档最初加载时加载,所以我认为这会导致问题。 Any thoughts really appreciated任何想法都非常感谢

Found the answer.找到了答案。 The form is being submitted via ajax.该表格正在通过 ajax 提交。 As such I need to copy the CKEditor data into the form field, which I do with因此,我需要将 CKEditor 数据复制到我使用的表单字段中

for (var instance in CKEDITOR.instances){
    CKEDITOR.instances[instance].updateElement();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM