问题
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 %}
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>
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 %}
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---------').
Does anyone have a suggestion?
{% 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 %}
回答1:
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}}
.
回答2:
Because a string that contains a username is not the same as a User
object with that username.
You can check it with:
{% if comment.name == user.username %}
<!-- … -->
{% endif %}
But that being said, storing the username is not a good idea. Imagine that the user later changes their username, then the comments are no longer "linked" to that user. Normally one makes use of a ForeignKey field [Django-doc] to refer to another model object. Most databases also enforce referential integrity, and thus ensure that if you refer to a user, that user exists at the table you are referring to.
It might thus be better to model you Comment
as:
from django.conf import settings
from django.db import models
class Comment(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
# …
来源:https://stackoverflow.com/questions/61621950/if-comment-name-user-not-working-correctly