简体   繁体   English

Django-tinyMCE 提交按钮不起作用

[英]Django-tinyMCE Submit button not working

I've implemented TinyMCE with the django-tinymce package.我已经用 django-tinymce package 实现了 TinyMCE。 However, my submit button which worked fine without TinyMCE now has become rather useless since I can't submit the form, once everything is filled out.但是,我的提交按钮在没有 TinyMCE 的情况下工作正常,现在变得相当无用,因为一旦填写了所有内容,我就无法提交表单。

I can use Ctrl + S inside of TinyMCE (I discovered that by accident) and everything will get submitted correctly.我可以在 TinyMCE 中使用 Ctrl + S (我偶然发现),一切都会正确提交。 Also, I can use the save-button of the TinyMCE "save" plugin to submit.. Do I have to configure the submit button to make it work with TinyMCE?另外,我可以使用 TinyMCE“保存”插件的保存按钮来提交。我是否必须配置提交按钮才能使其与 TinyMCE 一起使用?

Template:模板:

{% extends 'medisearch/header.html' %}
{% load crispy_forms_tags %}
{% block header %}
{{ form.media }}
{% endblock %}
{% block content %}
▷⋅⋅⋅⋅⋅⋅⋅<form action="{{ url }}" method="post">
▷⋅⋅⋅⋅⋅⋅⋅   <div class="form-group">
▷⋅⋅⋅⋅⋅⋅⋅    {% csrf_token %}
▷⋅⋅⋅⋅⋅⋅⋅    {{ form|crispy }}
▷⋅⋅⋅⋅⋅⋅⋅  </div>
▷⋅⋅⋅⋅⋅⋅⋅  <input type="submit" class="btn btn-primary" value="Speichern" />
▷⋅⋅⋅⋅⋅⋅⋅</form>
{% endblock %}

views.py视图.py

class EntryDetail(DetailView):
    model = Mediwiki
    slug_field = 'non_proprietary_name'
    template_name = 'mediwiki/entry.html'

class MediwikiForm(FormView):
    template_name = 'mediwiki/create.html'
    form_class = MediwikiForm⋅
    success_url = "/" #TODO user get's redirected to page he's created⋅

    def form_valid(self, form):
        form.save()
        return super(MediwikiForm, self).form_valid(form)

class EntryDisplay(View):
    def get(self, request, *args, **kwargs):
        try:
            view = EntryDetail.as_view()
            return view(request, *args, **kwargs)
        except Http404: # If there's no entry in db:
            if check_user_editor(request.user) == True:
                view = MediwikiForm.as_view()
                return view(request, *args, **kwargs)
            else:
                pass
    def post(self, request, *args, **kwargs):
        view = MediwikiForm.as_view()
        return view(request, *args, **kwargs)⋅

forms.py forms.py

class MediwikiForm(ModelForm):
    wiki_page = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
    class Meta:
        model = Mediwiki⋅
        fields = '__all__'

TinyMCE is in urls.py and under INSTALLED_APPS .. TinyMCE 在urls.pyINSTALLED_APPS下。

I know it's probably too late for you, but it seems that i had the same issue, just now and my solution might help someone in the future.我知道这对您来说可能为时已晚,但似乎我刚刚遇到了同样的问题,我的解决方案可能会在未来帮助某人。

You are using crispy, which includes the javascript files for the form on it's own.您正在使用crispy,它包含自己的表单的javascript文件。

Therefore the django_tinymce/init_tinymce.js will be referenced twice.因此 django_tinymce/init_tinymce.js 将被引用两次。 This will break the submittion of your content, since the form is initialized twice.这将中断您的内容的提交,因为表单被初始化了两次。

In order to fix this you may just remove the call of {{ form.media }}.为了解决这个问题,您可以删除 {{ form.media }} 的调用。

I had a similar issue and learned that it has to do with the way that TinyMCE deals with text areas.我有一个类似的问题,并了解到它与 TinyMCE 处理文本区域的方式有关。 The following init script worked for me:以下初始化脚本对我有用:

<script>
tinymce.init({
    selector:'.editor',
    setup: function (editor) {
        editor.on('submit', function (e) {
            editor.save();
        });
    }
 });
</script>

@artifex_knowledge answers makes sense and it works. @artifex_knowledge 的答案很有意义并且有效。 To build up on it, besides calling editor.save() on submit (or on change ), keep in mind that if users don't fill the text area, they won't be able to submit the form, but the this field is required error message won't be displayed.为了建立它,除了在submit (或change )时调用editor.save()之外,请记住,如果用户不填写文本区域,他们将无法提交表单,但是this field is required错误信息不会显示。

This is because the text area field (in this case wiki_page ) is required by default, so in the html it will be rendered with required .这是因为默认情况下需要文本区域字段(在本例中为wiki_page ),因此在 html 中它将使用required呈现。 But TinyMCE hides the text area (and replaces it with an iframe :( ) , so if you try to submit the form with an empty required, it won't, but the error message will keep hidden.但是TinyMCE隐藏了文本区域(并将其替换为iframe :( ) ,因此如果您尝试提交带有空要求的表单,它不会,但错误消息将保持隐藏。

(A possible solution is to use JS to remove the required attribute and check it in django later). (一种可能的解决方案是使用 JS 去除所需的属性,稍后在 django 中检查)。

只需从用作编辑器的textarea元素中删除required字段。

Deleting the 'required' field in the textarea element solved my problem (like Krysits mentioned)删除 textarea 元素中的“必填”字段解决了我的问题(如提到的 Krysits)

I also had the same issue as yours, and I just removed for instance: "wiki_page" charfield from the subclass of Modelform, and put Tinymce widget in the Meta class.我也遇到了和你一样的问题,我刚刚从 Modelform 的子类中删除了“wiki_page”字符域,并将 Tinymce 小部件放在 Meta 类中。

class MediwikiForm(ModelForm):
class Meta:
    model = Mediwiki⋅
    fields = '__all__'
    widgets = {
              'wiki_page': TinyMCE(attrs={'cols': 80, 'rows': 30})
             }

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

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