skip to Main Content

I want to using bootstrap wysiwyg in my Flask website.
I found this project flask-wysiwyg and followed the author’s advise, but it doesn’t work.

I would like to know:

  • What is wrong?
  • How should I solve this by myself?

Here is the code:

form.py

from .wysiwyg import WysiwygField

class PostForm(Form):
    article_title = StringField(u'title', validators=[Required(), Length(1, 64)])
    body = WysiwygField(u"txteditor", validators=[Required()])
    submit = SubmitField(u'submit')

view.py

# views.py
from .forms import PostForm
@main.route('/post', methods=['GET', 'POST'])
@login_required
def post():
    title = u'edit'
    form = PostForm()
    if  form.validate_on_submit():
        posts = Post(article_title=form.article_title.data,
                     body=form.body.data,
                     author=current_user._get_current_object())
        db.session.add(posts)
        flash(u'commit')
        return redirect(url_for('.doc'))
    return render_template('post-rich-text-edit.html', title=title, form=form)

post-rich-text-edit.html

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block head %}
{{ super() }}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='prettify.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='font-awesome.css') }}">


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
{% endblock %}

{% block page_content %}
<div class="page-header">
    <div class="row">
        <div class="col-md-12">
            <form method=post action="/">
            {{ form.article_title }}
                {{ form.hidden_tag() }}
                {{ form.body.label }}
                <div id="editor">
                {{ form.body(size=20) }}

                </div>
            {{ form.submit }}
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
    <script  type="text/javascript" src="{{ url_for('static', filename='bootstrap-wysiwyg.js') }}"></script>
    <script  type="text/javascript" src="{{ url_for('static', filename='jquery.hotkeys.js') }}"></script>
    <script  type="text/javascript" src="{{ url_for('static', filename='prettify.js') }}"></script>
{% endblock %}

3

Answers


  1. It’s not very clear what are the issues you are facing. However, you have a point that the documentation of the flask-wysiwyg package might be misleading.

    To use WysiwygField, change your import statement in forms.py to:

    from flask_wysiwyg.wysiwyg import WysiwygField
    
    Login or Signup to reply.
  2. Move the last 3 lines in scripts block to head block

    Login or Signup to reply.
  3. add WysiwygField to forms.py

    from flask_wysiwyg.wysiwyg import WysiwygField
    

    and add to block scripts in post-rich-text-edit.html

    <script>
        $('#editor').wysiwyg();
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search