Reputation: 187
Now I have a model called lyric. The detail is as below:
class Lyric(models.Model):
title = models.CharField(max_length = 200)
body = models.CharField(max_length = 12000)
pub_date = models.DateTimeField('date published')
user = models.OneToOneField(User)
I have a form that user can create lyric. Next I want to enable the user can edit and delete the lyrics. Now I have implemented the form of editing and the function of delete. But how can I limit the permission? Thank you in advance!!!
Upvotes: 1
Views: 1441
Reputation: 21834
Here's a quick hint. In your views, you can do this:
if request.user == lyric.user:
lyric.delete() # or save edits
else:
raise PermissionDenied # import it from django.core.exceptions
In your templates, you can access the logged in user as request.user
or simply user
:
{% if request.user == lyric.user %}
<a href="/link/to/view/">Delete</a>
{% endif %}
Upvotes: 5