如何将CreateView模板包含在ListView中?

2024-05-15 06:33:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让我的博客像Facebook一样,在Facebook上创建帖子的选项与帖子列表的视图相同。你知道吗

我试图在我的ListView中包含CreateView模板,但它给了我以下错误:

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>

我要做的只是在列表模板中包含create模板。如果这不起作用,我只想让用户在同一个页面上创建一个新的帖子,就像Facebook一样。你知道吗


Tags: 方法divform模板列表facebookhtml选项
1条回答
网友
1楼 · 发布于 2024-05-15 06:33:37

在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

然后你应该能够在模板中使用

相关问题 更多 >

    热门问题