如果comment.name ==用户无法正常工作 [英] if comment.name == user not working correctly

查看:53
本文介绍了如果comment.name ==用户无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够删除他们写的评论.不幸的是,尽管我无法使if语句起作用.

I want users to be able to delete comments they have written. Unfortunately though I can not get the if statement to work.

 {% if comment.name == user %}

        <a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
        {% endif %}

所以我可以看到user和comment.name的值,其中包括以下代码.这是出于测试目的,不会包含在站点的最终设计中.

So I could see the value of user and comment.name I included the below code. This was for testing purposes and would not be included in the sites final design.

<h3 class="article-content">{{ user  }}</h3>

<p class="article-content">{{ comment.name  }}</p>

我还想测试以确保if语句的内部正常工作.

I also wanted to test to make sure the inside of the if statement was work.

<a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
            {% endif %}

因此,我包含了以下代码.同样,这不会包含在我的最终网站设计中.

So I included the below code. Again this would not be included in my final sites design.

{% if post.author == user %}

        <a href="{% url 'delete_own_comment' comment.id %}">-------delete this comment---------</a>
        {% endif %}

但是通过查看屏幕截图,您可以判断代码是否正常运行.第30条注释是出于StackOverflow目的的测试注释".该评论由RossSymonds撰写.当前登录的用户是RossSymonds.如果一切正常,您将看到删除此评论"(与"------删除此评论---------"不同).

But by looking at the screenshot you can tell the code is not working properly. The 30th comment is a 'Test comment for StackOverflow purposes.' The comment was written by RossSymonds. The user currently logged in is RossSymonds. If everything was working properly you would see 'delete this comment' (which is different to '------delete this comment---------').

有人建议吗?

{% extends "blog/base.html" %}

{% block content %}
{% load crispy_forms_tags %}


  <article class="media content-section">
    <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
    <div class="media-body">
      <div class="article-metadata">
        <a class="mr-2" href="#">{{ post.author }}</a>
        <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
        {% if post.author == user %}
          <div>
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' post.id %}">Update</a>
            <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' post.id %}">Delete</a>
          </div>
        {% endif %}
      </div>
      <h2 class="article-title">{{ post.title }}</h2>
      <p class="article-content">{{ post.content }}</p>
    </div>
  </article>


<article class="media content-section">
        <!-- comments -->
         <h3>{{ comments.count }} Comments</h3>

  </article>


        <!-- comments -->


        {% for comment in comments %}
        <article class="media content-section">
        <div class="media-body">

            <small class="text-muted">{{ comment.id}}</small>
            <small class="text-muted">{{ comment.name}}</small>
            <small class="text-muted">{{ comment.created_on|date:"F d, Y" }}</small>
        <div class="media-body ">
          <p class="article-content">{{ comment.body  }}</p>

          <h3 class="article-content">{{ user  }}</h3>

          <p class="article-content">{{ comment.name  }}</p>
        </div>
        </div>

        {% if comment.name == user %}

        <a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
        {% endif %}


        {% if post.author == user %}

        <a href="{% url 'delete_own_comment' comment.id %}">-------delete this comment---------</a>
        {% endif %}




        </article>



        {% endfor %}




 {% if user.is_authenticated %}

 <form action="" method="post">

    {% csrf_token %}
    {{ comment_form|crispy }}
    <input type="submit" value="Submit">
</form>

{% else %}

<article class="media content-section">
Users who are logged in can post comments. 
</article>
{% endif %}

{% endblock content %}

推荐答案

您是否尝试过将 user 替换为 {%if comment.name中的 user.username ==用户%} 吗?
{{user}} 包含所有用户信息(链接到User模型的每个字段)
例如,如果要获取用户的用户名,则可以编写 {{user.username}} .

Have you tried replacing user to user.username in {% if comment.name == user %} ?
{{user}} contain all of the user's information (every fields linked to User model)
For instance, if you want to get the user's username, you can write {{user.username}}.

这篇关于如果comment.name ==用户无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆