以HTML格式显示数据

时间:2016-07-04 05:46:57

标签: django django-templates

假设我有一个名为" comment_list"的对象,它只是一个评论列表&相关信息我想将它显示在页面上,所以我这样做:

page.html中

{% for comment in comment_list %}
    <b>ID:{{ comment.id }} - {{ comment.name }} (on {{ comment.submit_date }})</b><br>
    <h4>{{ comment.comment }}</h4><br><br>
{% endfor %}

我为此使用了django-comments附加组件,它运行良好,完全没问题。现在,这就是事情 - 我想在评论中添加一个特殊功能,这样如果激活,这个特殊功能将代替该特定评论显示。我构建了一个连接到Comments的新表,并通过comment_id链接它。

我当时想要做的是实现&#34; if-else&#34; HTML中的东西在循环时替换注释。要清楚,&#34; comment_list&#34;内有多个对象。所以...:

page.html中

{% if special_feature.comment_id = comment.id %}
    <b>ID:{{ comment.id }} (on {{ comment.submit_date }})</b><br>
    <h4>YES SPECIAL FEATURE!</h4><br><br>
{% else %}
    <b>ID:{{ comment.id }} - {{ comment.name }} (on {{ comment.submit_date }})</b><br>
    <h4>{{ comment.comment }}</h4><br><br>
{% endif %}

&#34; special_feature&#34;内部还有多个对象。但我似乎无法使其发挥作用。 &#34; special_feature.comment_id&#34;不会出现,因为我希望它里面有多个对象。我必须明确说明&#34; special_feature.X.comment_id&#34;,其中&#34; X&#34;是一个数字。

因此解决方案是以某种方式使用&#34; forloop.counter&#34;为了那个原因。在这种情况下,最终代码可能如下所示:

page.html中

{% for comment in comment_list %}
    {% if special_feature.(forloop.counter|add:"-1").comment_id = comment.id %}
        <b>ID:{{ comment.id }} (on {{ comment.submit_date }})</b><br>
        <h4>YES SPECIAL FEATURE!</h4><br><br>
    {% else %}
        <b>ID:{{ comment.id }} - {{ comment.name }} (on {{ comment.submit_date }})</b><br>
        <h4>{{ comment.comment }}</h4><br><br>
    {% endif %}
{% endfor %}

&#34;添加:-1&#34;是这样的,计数器从&#34; 0&#34;而不是&#34; 1&#34;。 &#34; special_feature(forloop.counter |加入:&#34; -1&#34)。COMMENT_ID&#34;显然不起作用,有没有更好的方法来解决这个问题?

编辑:

这是view.py

@login_required(login_url='sign_in')
def display_comments(request, magazine_slug, art_id):
    context = {}
    populateContext(request, context)

    magazine_data = get_object_or_404(Magazine, slug=magazine_slug)

    article_data = get_object_or_404(Article, project_id=magazine_data.id, id=art_id)

    special_feature = CommentSpecial.objects.all().filter(user_id=request.user.id)

    context = {'article':article_data, 'magazine':magazine_data, 'special_feature':special_feature}



    return render(request, 'projsapp/page.html', context)


@login_required(login_url='sign_in')
def add_special_feature_comments(request, art_id):
    context = {}
    populateContext(request, context)

    # Acquire data from post
    data = request.POST
    special_type = data.get('id_special')
    date_time=datetime.datetime.now()

    # Confirm whether article exists
    article_data = get_object_or_404(Article, id=art_id)

    content_type = ContentType.objects.get_for_model(article_data)

    # Save a new comment manually
    save_comment=Comment.objects.create(object_pk=art_id, user_name="SYSTEM", user_email="lala@lala.com", comment="SPECIAL", ip_address="127.0.0.1", is_public=1, is_removed=0, content_type_id=content_type.id, site_id=settings.SITE_ID, user_id=request.user.id, submit_date=date_time)

    # Save to CommentSpecial as well!
    CommentSpecial.objects.create(special_type=0, user_id=request.user.id, text="SUPER", comment_id=save_comment.id)


    return HttpResponseR blah blah....

这里有关于CommentSpecial的model.py:

class CommentSpecial(models.Model):
    comment = models.ForeignKey(Comment)
    user_id = models.ForeignKey(User)

    creation_date = models.DateTimeField(auto_now_add=True)
    text = models.CharField(max_length=100, blank=True, null=True, default='')
    special_type = models.BooleanField(default=False)
    # False = Super Special, True = Typical Special

0 个答案:

没有答案