Django类视图

关键字

  • response
  • self
  • request
  • object


# model.py
class HostComment(models.Model):
    host = models.ForeignKey(Host, related_name='hostcomments')
    body = models.CharField(max_length=100, verbose_name='操作记录')
    created = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=20, default='admin')

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return 'Comment {} {} on {}'.format(self.host, self.body, self.created)



# views.py
class HostDetailView(DetailView):
    model = Host
    template_name = 'app/host_detail.html'
    context_object_name = 'post'

    def post(self, request, *args, **kwargs):
        response = super(HostDetailView, self).get(request, *args, **kwargs)
        comment_form = HostCommentForm(data=self.request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.host = self.object
            new_comment.author = self.request.user
            new_comment.save()
        return response


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        comment_form = HostCommentForm()
        comments = self.object.hostcomments.all()
        context['comments'] = comments
        context['comment_form'] = comment_form
        return context


# temples

      <div class="panel panel-warning">
                    <div class="panel-heading">操作记录</div>
                    <div class="panel-body">
                        {#操作记录输入框#}
                        <form action="{{ post.get_absolute_url }}" method="post">
                            {{ comment_form.as_p }}
                            {% csrf_token %}
                            <p><input type="submit" value="提交"></p>
                        </form>
                        {#操作记录显示框#}
                    <div class="profile-user-info profile-user-info-striped">
                        {% for comment in comments %}
                            <div class="profile-info-row">
                                <div class="profile-info-name">{{ comment.created|timesince }} 前 by {{ comment.author }}</div>
                                <div class="profile-info-value">
                                    <span>{{ comment.body|linebreaks }}</span>
                                </div>
                            </div>
                        {% endfor %}
                        </div>
                    </div>
                </div>
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Django基于类的视图 1.基于类的视图简介 基于类的视图使用Python 对象实现视图,它提供除函数视图之外的...
    常大鹏阅读 8,679评论 0 25
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • Refer to: www.threemeal.com/blog/12/ 中间件 中间件是一个钩子框架,它们可以介...
    兰山小亭阅读 16,580评论 9 165
  • 基于类的视图 Django中的视图是一个可调用对象,它接收一个请求然后返回一个响应。这个可调用对象不仅仅限于函数,...
    兰山小亭阅读 4,616评论 1 13
  • 中间件是一个钩子框架,它们可以介入Django 的请求和响应处理过程。它是一个轻量级、底层的“插件”系统,用于在全...
    低吟浅唱1990阅读 537评论 0 0