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

189
Views
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 answers
Answer question

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 Report

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 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!