Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
Django writing generic update view restricted to certain user

I am building a small blog using django.I want to build a function that allow post author to delete and update their own posts. Then I find django has LoginMixin for generic view,but it only block those who don't login.

My article Model is like below

class Article(models.Model):
    author = models.ForeignKey(User)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=50)
    context = models.TextField()

    genre_choice = (('O','Others'),('P','Programming'),('L','Learning'),('E','Entertainment'))

    genre = models.CharField(max_length=2,choices=genre_choice,default='O')
    def __str__(self):
        return "{} - {}".format(self.title,self.author)

    def get_absolute_url(self):
        return reverse("article-detail",args=str(self.id))

This is the generic article detail view.

class ArticleDetail(DetailView):
    model = Article

I firstly want to add something like this in the detail template:

{% if article.author == user.username%}
 <!-- Some a tag that directs to the update view -->
{% endif %}

Then I realize that this just hides the a tag ,it can't stop other users to touch the update url simply change the url.

Is there anyway in django can restricted the update and delete permissions to the original user by simply using generic view?Even if they directly enter the update url,they will be rejected.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Override get_queryset in your UpdateView, so that the user can only access items that they authored. Use the LoginRequiredMixin to ensure that only logged-in users can access the view.

from django.contrib.auth.mixins import LoginRequiredMixin

class UpdateArticle(LoginRequiredMixin, UpdateView):
    model = Article

    def get_queryset(self):
        queryset = super(UpdateArticle, self).get_queryset()
        queryset = queryset.filter(author=self.request.user)
        return queryset

In the template, I would compare the author_id with the user's primary key to decide whether to show the link or not.

{% if article.author_id == user.pk %}
about 4 years ago · Santiago Trujillo Report

0

One option is to create your own mixin/decorator to check if the logged user is the author, if not then reload/show a message etc.

about 4 years ago · Santiago Trujillo Report

0

I believe a safer way now would be to use built-in mixin UserPassesTestMixin. In particular, you can inherit it in your class and change its test_func() to check for the author. Don't forget to also inherit LoginRequiredMixin to make sure the user is logged in:

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

class UpdateArticle(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Article

    def test_func(self):
        thisArticle = self.get_object()
        if self.request.user == thisArticle.author:
            return True
        return False

If a user who is not the author attempts to update the article, '403 Forbidden' Error is returned which is just what you want in such a situation.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!