我正在使用Flask,mongoengine进行项目,我正在尝试从http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/
获取基本的东西在实现上述链接中的所有内容之后,我在Post中为“标签”添加了一个新字段,当我尝试创建帖子时,我的标签没有显示输入框。
感谢任何帮助。
我的代码
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 %}
admin.py
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'))
答案 0 :(得分:1)
由于列表字段可能为每个项目都有自己的特殊类型,因此您可能需要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")