Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

389
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda