miguelgrinberg / Flask-PageDown

Implementation of StackOverflow's "PageDown" markdown editor for Flask and Flask-WTF.
MIT License
244 stars 21 forks source link

Rendering PageDown fields separately with Flask-Bootstrap #18

Closed ffhan closed 5 years ago

ffhan commented 7 years ago

I am trying to render a form which has PageDownField inside of itself, but not with wtf.quick_form.

When I try rendering separate form fields like this:

{% block title %}Write a post!{% endblock %}

{% block scripts %} {{ super() }} {{ pagedown.include_pagedown() }} {% endblock %}

{% block fill_content %}

{{ form.hidden_tag() }} {{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.title, form_type = 'horizontal') }} {{ wtf.form_field(form.body(only_input = True, rows = 20)) }}
{{ wtf.form_field(form.body(only_preview = True)) }}

{% endblock %}

* *main/views.py*
```python
#.....
@main.route('/post', methods = ['GET', 'POST'])
@login_required
def write_post():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit():
        post = Post(title = form.title.data, body = form.body.data, author = current_user._get_current_object())
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.see_post', id = post.id))
    return render_template('post/write_post.html', form = form)
#....

The problem seems in template, specifically in {{ wtf.form_field(form.body(only_input = True, rows = 20)) }} and I don't exactly know why. I am aware that the convention is to name PageDownField element pagedown and use it accordingly, but that shouldn't be a problem.

The error that I get on lines wtf.form_field(form.body(...)) is

jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'wtforms.widgets.core.HTMLString object' has no attribute 'flags'

which is being raised by


File "D:\Programs\Python3\lib\site-packages\flask_bootstrap\templates\bootstrap\wtf.html", line 36, in template
                    horizontal_columns=('lg', 2, 10),
                    button_map={}) %}

{# this is a workaround hack for the more straightforward-code of just passing required=required parameter. older versions of wtforms do not have
the necessary fix for required=False attributes, but will also not set the required flag in the first place. we skirt the issue using the code below #}
{% if field.flags.required and not required in kwargs %}
{% set kwargs = dict(required=True, **kwargs) %}
Open an interactive python shell in this frame{% endif %}

So my guess is that Flask-Bootstrap doesn't like Flask-PageDown? To be clear, the reason I'm trying to render those elements with wtf.form_field is that I want bootstrap theme applied to my custom form fields (I don't want to use wtf.quick_form() here)

miguelgrinberg commented 7 years ago

Well, the argument to wtf.form_field() should be a field object, but instead you are passing a rendered HTML version of the object. It does not seem possible to use the only_preview and only_input options with Flask-Bootstrap, I did not consider that usage, I would need to make some changes to enable that to work.

miguelgrinberg commented 5 years ago

This issue will be automatically closed due to being inactive for more than six months. Please reopen if you need more assistance.