After using for loop I get a specific comment and its related user and only want to delete the comment if that user has written it. How to write variable inside {% if ---%}.
Template error:
In template C:\Users\SHAFQUET NAGHMI\socialnetwork\socialapp\templates\socialapp\comment.html, error at line 27
Could not parse the remainder: '==comm.user.id' from 'request.user.id==comm.user.id'
17 : <P>Please <a href="{% url 'login' %}">login</a> to add comment </P>
18 :
19 : {% endif %}
20 : </form>
21 : <h3>comments..</h3>
22 :
23 : {% for comm in comments %}
24 :
25 : <a href="{% url 'profile' comm.user.username %}">{{comm.user}}</a>
26 : {{comm.comment}}
27 : {% if request.user.id==comm.user.id %}
28 : <a href="/delete_comment/{{post.id}}/{{comm.id}}/">Delete</a>
29 : {% endif %}
30 : <br><br>
31 : {% endfor %}
32 : <!--{{post}} {{comm.id}}-->
33 :
34 : </div>
35 : </div>
36 : {% endblock %}
CodePudding user response:
The only way to use a variable is to use the with
tag. Example:
{% with name="World" %}
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}
However this may not be possible in your case. I would rather recommend having a custom property on the comment sent as part of the request, such that you can use comm.can_delete
to check if the user can delete the comment. Add the can_delete
property to your backend where you set it's value to be request.user.id == comm.user.id
as you need.
I don't know how your backend looks, so I can't tell you how to do it, but hope you understand my point.
CodePudding user response:
Perhaps you are missing some spaces?
{% if request.user.id == comm.user.id %}