S
S
Sergey Nizhny Novgorod2016-07-21 03:10:27
Django
Sergey Nizhny Novgorod, 2016-07-21 03:10:27

How to output CKeditor to form in Django?

Hello.
Can you suggest the logic of how to display CKeditor in a form template? I do according to their manual, but the editor does not appear in the template, only in the admin panel. It seems to me that I do not understand and miss something.
What am I doing:

from django import forms
from ckeditor.widgets import CKEditorWidget

class Topic_Form(forms.Form):
    topic_title = forms.CharField(label='Заголовок темы')    
    topic_text = forms.CharField(widget=CKEditorWidget, label='Ваш вопрос')

view
def forum_new_topic(request, topic_id):

    form = Topic_Form(request.POST)

    forum_category = str(topic_id)

    context = {
        "forum_category" : forum_category,
        "form" : form,
    }
    return render(request, 'faceset/forum_new_topiс.html', context)

Sample:
<form action="/#" method="post">
    {% csrf_token %}    
    {{ form.topic_title.label_tag }}
    {{ form.topic_title }}
    {{ form.topic_text.label_tag }}
    {{ form.topic_text.media }}                                
    <input type="submit" class="expanded button" value="ОПУБЛИКОВАТЬ">
</form>

And it doesn't work.
Am I doing something wrong?
##############
The minimum page code that the editor shows. It doesn't show like this:
{% load staticfiles %}
{% load banner %}

<!doctype html>
<!--suppress ALL -->
<html class="no-js" lang="ru">
<head>
</head>
<body>



<div class="row">

    <form action="/#" method="post">
        {% csrf_token %}
        {{ form.media }}

        <input type="hidden" name="category" value="{{ forum_category }}">
        <input type="submit" class="expanded button" value="ОПУБЛИКОВАТЬ">
    </form>

</div>


</body>
</html>

Добавляем так и работает.
{{ form.media }}
{{ form.as_p }} +++

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Keofaste, 2016-07-21
@Terras

forms.py:

from ckeditor.widgets import CKEditorWidget
from django import forms


class PostEditForm(forms.Form):
    content = forms.CharField(widget=CKEditorWidget, label='')

In template:
<form ...>
    {{ post_form.media }}
    ...
</form>

views.py:
def view(request):

    post_form= PostEditForm(request.POST)

    context = {
        "post_form" : post_form,
    }
    return render(request, 'template.html', context)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question