1

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)
4
  • @PrakharTrivedi one last question. When changing the if statement to message.messageuser.id its not equal to the request.user.id, But it is when I use message.user.id. The message.user.id give me an anonymous user error. Commented Nov 21, 2016 at 5:34
  • I don't understand your comment.Can you please explain more ? Commented Nov 21, 2016 at 5:38
  • Of course. When i use the message.messageuser.id == request.user.id. the delete button disappears again. If I use message.user.id the delete button shows up, but give me 'AnonymousUser' object is not iterable error. Do I need to change something in my urls or view to make message.messageuser.id == request.user.id? Commented Nov 21, 2016 at 5:48
  • Let us continue this discussion in chat. Commented Nov 21, 2016 at 5:49

2 Answers 2

4

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

And a plus 1 for being less lazy than I.
@Pythonista Ha ha..Good one :D
2

It seems unlikely that message.id will be equal to the request.user object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.