如何在ListView中包含CreateView模板?

时间:2019-05-04 16:05:09

标签: python django

我想使我的博客像Facebook,在其中创建帖子的选项与帖子列表处于同一视图。

我试图将CreateView模板包括在ListView中,但这给了我这个错误:

Parameter "form" should contain a valid Django Form.

我也尝试在评论中使用相同的方法。这种方法有效吗?或者还有其他方法吗?

这是post_list.html模板:

{% extends "posts/post_base.html" %}
{% load bootstrap4 %}
{% block post_content %}

  <div class="col-md-6">
    {% for post in post_list %}

       {% include "posts/_post.html" %}
       {% inclide 'posts/_create_post.html %}

    {% endfor %}
   </div>

{% endblock %}

这是create_post.html模板:

{% load bootstrap4 %}

<form  method="POST">
    {% csrf_token %}
    {% bootstrap_form form %}

    <input type="submit" name="" value="comment">
</form>

我要做的只是将创建模板包括在列表模板中。如果这行不通,我只想让用户在与Facebook之类的帖子列表相同的页面上创建一个新帖子。

1 个答案:

答案 0 :(得分:0)

在ListView中,您需要添加表单

def get_context_data(self, *args, **kwargs):
    ctx = super().get_context_data(*args, **kwargs)
    ctx['form'] = myForm(self.request.POST or None)
    return ctx

那么您应该可以在模板中使用