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

203
Visualizações
display edit button only if the logged in user is same as the user's page?

example:

1.example.com/username1

2.example.com/username2

i want to take to user to the edit profile page, i got all the permission working correctly and set the views accordingly.

My Problem:

i loggedin as username1 so i should not be able to access example.com/username2/edit/. i got this to work in the views by adding some permissions, but i dont want to display the edit button when the loggedin username1 views the page of username2. Now i use {% if user.is_authenticated %} and this results in display the edit button on page of username2 even though the loggedin user is username1.

Any simple solutions that can be used in the templates directly?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

views.py

class CanEditMixin(object):
    def get_context_data(self, **kwargs):
        """
        The method populates Context with can_edit var
        """
        # Call the base implementation first to get a context
        context = super(CanEditMixin, self).get_context_data(**kwargs)
        #Update Context with the can_edit
        #Your logic goes here (something like that)
        if self.request.user.slug == self.kwargs['slug']:
            context['can_edit']=True
        else:
            context['can_edit']=False
        return context

class ProfileView(CanEditMixin, UserPassesTestMixin, DetailView): .....

template.html

{% if can_edit %}
    <a href="#">Edit</a>
{% endif %}
over 4 years ago · Santiago Trujillo Relatório

0

you can check if request.user is the same as the user that the URL is assigned to. In your request you have a key called 'user' which is your logged one (if logged) or anonymous (if not logged).

{% if user.is_authenticated %} is for checking if the user is authenticated (as the name says), that means if the user is a valid user (for example he clicked the link in the email).

over 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