Jives Jives - 11 days ago 5
Python Question

Cannot get my delete button link to show up in django

I can't figure out what is wrong with my code to get my delete message link to show up. All help is greatly appreciated.

views.py

def remove_message(request, message_id):
Message.objects.filter(id=message_id, user=request.user).delete()
return redirect(reverse('dashboard:show', args=user_id))


show.html

{% for message in messages%}
<div class="message">
<p class='bg-primary wall_content'><strong>{{message.messageuser.first_name}} wrote:</strong></p>
<p class='wall_content'>{{message.message}}</p>
{% if message.id == request.user %}
<a href='{% url "dashboard:remove_message" message.id %}'>Delete Message</a>
{% endif %}
{% endfor %}


message model:

class Message(models.Model):
message = models.CharField(max_length=1000)
walluser = models.ForeignKey(User, related_name='userwall')
messageuser = models.ForeignKey(User, related_name='usermessage')
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

Answer

The problem is in your template code,in the if condition :

{% if message.id == request.user %}

Here message.id in always not equal to request.user So you need to check this condition and update your code according to your models. Something like this :

{% for message in messages%}
<div class="message">
    <p class='bg-primary wall_content'><strong>{{message.messageuser.first_name}} wrote:</strong></p>
    <p class='wall_content'>{{message.message}}</p>
    {% if message.messageuser.id == request.user.id %}
    <a href='{% url "dashboard:remove_message" message.id %}'>Delete Message</a>
    {% endif %}
{% endfor %}
Comments