Monday, June 29, 2015

How to set back the data to ck-editor instance in a Django web app?

I have a Django web app which has various instances of ck-editor instances on a web page.By using on blur event I am saving whole of data of the instance in the database -
{% for editor in editors %}
<div id="{{editor.ck_id}}" name="{{editor.ck_id}}">
</div>
{% endfor %}

<script>
{% for editor in editors %}
   CKEDITOR.appendTo("{{editor.ck_id}}" ,
    {
        on: {
            blur: function(event){
                var data = event.editor.getData();
                console.log("data of {{editor.ck_id}} is " + data);

                 var request = $.ajax({
                     url: "/
editor/save/",
                     type: "
GET",
                     data: {
                         content : data,
                         content_id : "
{{editor.ck_id}}"
                    },
                     dataType: "
html"
                 });
            }
        }

    },


    "
{{editor.data}}"
    );
{% endfor %}

Here ck_id and data are the two database fields of ckeditors.Django model is -

from django.db import models

class ckeditor(models.Model):
    ck_id
= models.CharField(max_length=100,null=False,blank=False)
    data
= models.TextField(null=True,blank=True)

I have stored editor's data as a text field.Now suppose I write this on one instance of ckeditor -

Tony Stark

     
is Iron man.

Editor Image is -





Editor source is - 


It's weird that ck-editor does not have 
tag in source code(HTML format).Now when AJAX call is made I handle it like this -

def save(request):
if request.method == 'GET':
    editor_data
= request.GET['content']
    editor_id
= request.GET['content_id']

   
print "data received"
   
print "editor data is %s" %(editor_data)
   
print "editor id is %s" %(editor_id)
else:
   
print "No Data received"

editor
= ckeditor.objects.get(ck_id=editor_id)
editor
.data = editor_data
editor
.save()

In database data looks like this -


One can see that the html is rendered as text field in the database.Now however when I query editor's data in python shell it shows like this -

One can see now that since it was a text field it has filled some '\n' tags in between.

Now When I re start the server and set the data of every ck-editor instance again then this exception is raised -

Uncaught SyntaxError: Unexpected token ILLEGAL


This happened because the text(editor's data) which I sent got converted into illegal tokens.Do I need to convert text which I am sending into valid html or do I need to encode/decode that text?

Now my question is how to go about this complete procedure of fetching editor's data,storing it in database and then again reset data on restarting server. I have posted this question on several forums but many people have got no clue of what I am asking?

CK-EDITOR team has proudly mentioned that their community has been shifted to SO but I have got no replies from them either. I guess they only give support when user buys their license which costs 299$.

Can anybody please help me out in this? I am literally lost all hopes.I am getting no help for last 1 week and probably I will get some help this time around.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02d4646d-2435-45e4-8ee6-8859b09c8451%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment