0

I'm working on an project that let users create blogs and allow other users to comment on each other blogs.When a user creates a blog , their are certain ability that the blog owner can do such as deleting their own comments and the way I'm deleting comments is via hyperlink passing value id . Everyone can see each other blogs but I want to only show the deletion hyperlink to the owner of the blog , so only the user who created the blog. How can I do this? via template

My models

class Blog(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    description = models.TextField()

def Bloglook(request ,animal_id):
        Blog = Blog.objects.get(pk=animal_id)
    return render(request,'blog.html',{blog':blog})

my blog.html

{% if blog %}
    {{blog.name%}}
{% endif %}

How Can I only show this link to the person who created the blog?

<a  href="{% url world:BlogDelete blog.id %}"> Delete blog</a>
4

2 回答 2

1
{% if request.user==blog.user %}<a  href="{% url world:BlogDelete blog.id %}"> Delete blog</a>{% endif %}

Edited:

This will also be hidden from unautenticated users. Only if user is the owner then he will see the deletion link

Also, you can continue using render, there is no need to change to render_to_response.

于 2013-04-27T06:06:45.993 回答
1

Use RequestContext to pass to template which passes request.user variable to template, which you can use to check for owner of blog.

Change your view to use RequestContext as

def Bloglook(request ,animal_id):
    Blog = Blog.objects.get(pk=animal_id)
    return render_to_response('blog.html',{blog':blog}, 
             context_instance = RequestContext(request))

Then in template do

{% if blog.owner == request.user %}
     <a  href="{% url world:BlogDelete blog.id %}"> Delete blog</a>
{%endif%}
于 2013-04-27T06:07:19.917 回答