ListField显示< ul>而不是<输入>在编辑/创建帖子 [英] ListField is showing <ul> instead of <input> in edit/create post

查看:301
本文介绍了ListField显示< ul>而不是<输入>在编辑/创建帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask,mongoengine作为一个项目,我试图从文档,它似乎是 ListField 只是一个 FieldList as WTForms引用它

目前,您并没有在WTForms中独立地定义表单,而只是使用包含在Flask-MongoEngine,所以我的第一个尝试是添加一些更多的逻辑到你的宏中,添加一个 {%elif field.type =='ListField'%} 发现里面包含的内容来迭代生成表单。从源代码的快速浏览,类似下面的东西可能工作。

pre $ {$ elif field.type == '%1 $'$ {$%$ $ $ $ {$%$'$'$% render_the_subfield#}
{%endif%}
{%end for%}
...

该代码需要处理,但希望它会指向正确的方向。否则,我实际上在WTForms中单独定义了表单,为您在代码方面提供了更多的控制权。幸运的是,他们提供 csv标记示例,如果您需要走这条路。 我写了一本指南

a>,它使用 @property 装饰器的不同路径来达到类似的效果,至少可以指向终点线。 b

I am using Flask, mongoengine for a project and I am trying to get basic stuff working from http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/

After implementing everything from above link I added a new field for "tags" in Post and when I try to create a post, my tags doesn't show a input box.

Any help is appreciated.

My code and screenshot below

class Post(db.DynamicDocument):
    created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
    title = db.StringField(max_length=255, required=True)
    slug = db.StringField(max_length=255, required=True)
    comments = db.ListField(db.EmbeddedDocumentField('Comment'))
    tags = db.ListField(db.StringField(max_length=30)) # New field I added

template form

{% macro render(form) -%}
<fieldset>
{% for field in form %}
{% if field.type in ['CSRFTokenField', 'HiddenField'] %}
   {{ field() }}
{% else %}
  <div class="clearfix {% if field.errors %}error{% endif %}">
    {{ field.label }}
    <div class="input">
      {% if field.name == "body" %}
        {{ field(rows=10, cols=40) }}
      {% else %}
        {{ field() }}
      {% endif %}
      {% if field.errors or field.help_text %}
        <span class="help-inline">
        {% if field.errors %}
          {{ field.errors|join(' ') }}
        {% else %}
          {{ field.help_text }}
        {% endif %}
        </span>
      {% endif %}
    </div>
  </div>
{% endif %}
{% endfor %}
</fieldset>
{% endmacro %}

rendering form code

{% extends "admin/base.html" %}
{% import "_forms.html" as forms %}

{% block content %}
<h2>
  {% if create %}
  Add new Post
  {% else %}
  Edit Post
  {% endif %}
</h2>

<form action="?{{ request.query_string }}" method="post">
  {{ forms.render(form) }}
  <div class="actions">
    <input type="submit" class="btn primary" value="save">
    <a href="{{ url_for("admin.index") }}" class="btn secondary">Cancel</a>
  </div>
</form>
{% endblock %}

解决方案

From what I can gather, your problem is you're telling WTF to render the tags field, but WTForms doesn't know how to handle that information.

From looking at the Flask-MongoEngine documentation, it seems the ListField is just a FieldList as WTForms refers to it.

Currently you're not actually defining the form independently in WTForms, you're just using the magic included in Flask-MongoEngine, so my first attempt would be to add some more logic to your macro, add a {% elif field.type == 'ListField' %} and try and discover what's contained in there to iterate through to produce your form. From having a quick look at the source-code, something like the following might work.

{% elif field.type == 'ListField %}
    {# render_the_group_label #}
    {% for subfield in field.entries %}
        {% if subfield.type == 'StringField' %}
            {# render_the_subfield #}
        {% endif %}
    {% endfor %}
...

That code will need to be worked on, but hopefully it'll point you in the right direction. Otherwise, I'd actually define the form seperately in WTForms to give you a bit more control on the code-side. Luckily they provide a csv tag example which should help you if you need to go that route. I wrote a guide that takes a different route using @property decorators to achieve a similar effect, which again, might at least point you towards the finish line.

这篇关于ListField显示&lt; ul&gt;而不是&lt;输入&gt;在编辑/创建帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆