我有以下表单在自己的视图中工作,我正在尝试将其设置为模板标记。
@login_required
@register.inclusion_tag('posts/new_post_form.html', takes_context = True)
def newpostform(context):
request = context['request']
if request.user.is_authenticated():
post_form_local = PostFormLocal(request.POST or None)
user = request.user
if post_form_local.is_valid():
save_form = post_form_local.save(commit=False)
save_form.user = request.user
save_form.save()
messages.success(request, 'Post Saved')
return HttpResponseRedirect('/post/%d/' % save_form.id)
else:
return {'post_form_local': PostFormLocal(), 'user': user}
通过致电:
,表格会在页面上显示{% load newpost %}
{% newpostform %}
(我正在使用Crispy Forms)
new_post_form.html看起来像:
{% load url from future %}
{% load crispy_forms_tags %}
<form method="POST" action=""> {% csrf_token %}
<h3>Create New Post</h3>
{% crispy post_form_local %}
</form>
表单显示正常,提交,但随后出现错误:
VariableDoesNotExist at ...(当前页面)
在[&#34; False&#39;:False,&#39;无&#39;:无,&#39; True&#39;:True}中查找键[post_form_local]失败,]&#34;
如果我刷新此页面,帖子会保存并显示为正常 - 无法弄清楚我在这里缺少什么,有什么想法吗?