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?
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 %}
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).