我正在为项目使用Mongoengine的Flask,并且我正在尝试从http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/中获取基本内容
在实现了以上链接中的所有内容之后,我在“帖子”中添加了一个用于“标签”的新字段,当我尝试创建帖子时,我的标签未显示输入框。
任何帮助表示赞赏。
我的代码如下
class Post(db.Document):
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)
body = db.StringField(required=True)
views = db.IntField(default=0)
category = db.StringField()
tags = db.ListField(db.StringField(max_length=30))
模板
{% 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.type == 'ListField' %}
{% for subfield in field.entires %}
{% if subfield.type == 'StringField' %}
{{ field }}
{% endif %}
{% endfor %}
{% elif field.name == "body" %}
{{ field(rows=10, style="width:360px;") }}
{% 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 %}
管理员
from flask import Blueprint, request, redirect, render_template, url_for
from flask.views import MethodView
from pprint import pprint
from flask.ext.mongoengine.wtf import model_form
from tumblog.auth import requires_auth
from tumblog.models import Post
admin = Blueprint('admin', __name__, template_folder='templates')
class List(MethodView):
decorators = [requires_auth]
cls = Post
def get(self,slug=None):
posts = self.cls.objects.all()
return render_template('admin/list.html', posts=posts)
class delList(MethodView):
decorators = [requires_auth]
cls = Post
def get(self,slug=None):
try:
post = self.cls.objects.get(slug=slug)
post.delete()
except:
pass
return redirect(url_for('admin.index'))
class Detail(MethodView):
decorators = [requires_auth]
def get_context(self, slug=None):
form_cls = model_form(Post, exclude=('created_at', 'views'))
if slug:
post = Post.objects.get_or_404(slug=slug)
if request.method == 'POST':
form = form_cls(request.form, inital=post._data)
else:
form = form_cls(obj=post)
else:
post = Post()
form = form_cls(request.form)
context = {
"post": post,
"form": form,
"create": slug is None
}
return context
def get(self, slug):
context = self.get_context(slug)
return render_template('admin/detail.html', **context)
def post(self, slug):
context = self.get_context(slug)
form = context.get('form')
if form.validate():
post = context.get('post')
form.populate_obj(post)
post.save()
return redirect(url_for('admin.index'))
return render_template('admin/detail.html', **context)
# Register the urls
admin.add_url_rule('/admin/', view_func=List.as_view('index'))
admin.add_url_rule('/admin/create/', defaults={'slug': None}, view_func=Detail.as_view('create'))
admin.add_url_rule('/admin/edit/<slug>/', view_func=Detail.as_view('edit'))
admin.add_url_rule('/admin/delete/<slug>/', view_func=delList.as_view('delete'))
最佳答案
因为列表字段可能对每个项目都有自己的特殊类型,所以您可能需要define custom fields;或者您可以尝试直接使用 wtforms.fields.FieldList
。
很难说出您遇到了哪个错误。您可以尝试将信息放入表单中进行调试,例如
{% for field in form %}
<!-- {{field.type}} {{field.label}} -->
... etc ...
{% endfor %}
还注意到您有错字
{% for subfield in field.entires %} <=== did you mean field.entries ?
赏金问题:
除非在声明字段时定义
help_text
,否则将无法按预期工作:{{ field.help_text }}
您在声明字段时需要将其包括在内,例如
oid = ObjectIdField(..., help_text="object id")
关于python - 在MongoEngine上渲染ListField Model_form时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20852184/