这是错误:
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1701, in __call__ return self.wsgi_app(environ, start_response)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1689, in wsgi_app response =self.make_response(self.handle_exception(e))
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1687, in wsgi_app response = self.full_dispatch_request()
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1360, in full_dispatch_request rv = self.handle_user_exception(e)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1358, in full_dispatch_request rv = self.dispatch_request()
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/app.py",
line 1344, in dispatch_request return
self.view_functions[rule.endpoint](**req.view_args)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/views.py",
line 83, in view return self.dispatch_request(*args, **kwargs)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/views.py",
line 150, in dispatch_request return meth(*args, **kwargs)
File"/www/sites/MyBlog/app/posts.py", line 38, in get return
render_template('posts/detail.html', **context)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/templating.py",
line 125, in render_template context, ctx.app)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/flask/templating.py",
line 107, in _render rv = template.render(context)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/jinja2/environment.py",
line 894, in render return
self.environment.handle_exception(exc_info, True)
File"/www/sites/MyBlog/app/templates/posts/detail.html", line 9, in
toplevel template code {% block content %}
File"/www/sites/MyBlog/app/templates/posts/detail.html", line 23, in
block "content" {{ forms.render(form) }}
File"/www/sites/MyBlog/app/templates/_forms.html", line 13, in template
{{ field() }}
File"/www/sites/blog/flask/lib/python2.7/sitepackages/wtforms/fields/core.py",
line 140, in __call__ return self.widget(self, **kwargs)
File"/www/sites/blog/flask/lib/python2.7/sitepackages/wtforms/widgets/core.py",
line 246, in __call__ for val, label, selected in field.iter_choices():
File"/www/sites/blog/flask/lib/python2.7/sitepackages/wtforms/ext/sqlalchemy/fields.py",
line 108, in iter_choices for pk, obj in self._get_object_list():
File"/www/sites/blog/flask/lib/python2.7/sitepackages/wtforms/ext/sqlalchemy/fields.py",
line 99, in _get_object_list Display the sourcecode for this
frameOpen an interactive python shell in this framequery = self.query
or self.query_factory()
File"/www/sites/blog/flask/lib/python2.7/sitepackages/wtforms/ext/sqlalchemy/orm.py",
line 117, in <lambda> 'query_factory': lambda:
db_session.query(foreign_model).all() AttributeError: type object
'Form' has no attribute 'query'
这是我的view.py方法
class DetailView(MethodView):
def get_context(self, slug):
p = Post('','',slug)
post = p.get_or_404(slug)
form = model_form(Comment, Form, exclude=['timestamp'])(request.form)
context = {
"post": post,
"form": form
}
return context
def get(self, slug):
context = self.get_context(slug)
return render_template('posts/detail.html', **context)
def post(self, slug):
context = self.get_context(slug)
form = context.get('form')
if form.validate_on_submit():
comment = Comment()
form.populate_obj(comment)
post = context.get('post')
post.comments.append(comment)
db.session.commit()
return redirect(url_for('posts.detail', slug=slug))
return render_template('posts/detail.html', **context)
,这是我的表单模板:
{% 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 %}
我收到与以下代码相关的错误
form = model_form(Comment, Form, exclude=['timestamp'])(request.form)
并
{{ field() }}
请提出一些解决方案。提前致谢。
答案 0 :(得分:5)
我会发布我的解决方案,即使它并不完美。
在努力解决这个问题之后,我发现当db_session
尝试从数据库进行查询时,当foreign_model
不是我正在编辑的模型时,它会失败,这实际上是我的关系。
SLOVED BY
exclude = ['user', 'offers', 'languages', 'company']
MyForm = model_form(Driver, Form, exclude=exclude)
在我的模型中,我必须排除所有关系。工作正常,仍然不知道如何编辑它们,但在我的情况下,我没有必要。
<强>更新强>
我设法通过传递我的会话来解决它,而不是像这样的Form
:
MyForm = model_form(Driver, db_session=db.session)
form = MyForm(request.form, driver)