Passing a value in anchor tag in django

So I'm in my profile page & want to edit my profile so I click edit button ,it takes me to a edit page where I have two buttons 'save'(inside form tag) & 'Exit'. Now if I click exit I want to get redirected to profile page of same person again. what input should I pass in below tag to do this?

(<a href="{% url 'profile'  %}"><button>Exit</button></a>

For going back to particular profile you will need a url with profile/int:id and have to pass unique id of that particular profile in your url while calling exit.

Below is the sample you can do it where profile.id contains value of id of a profile.

<a href="{% url 'profile' id=profile.id %}"><button>Exit</button></a>

i am not agree with other answers. If you on your profile page, it means you are logged in.

In this case - you don't need to define user.pk, and it is better for usability - i can simply save the link mydomain/profile instead of mydomain/profile/user_pk.

But you should define, how you get an user from Request.

class ProfileView(LoginRequiredMixin, DetailView):
    model = User

    def get_object(self, *args, **kwargs):
        self.kwargs['pk'] = self.request.user.pk
        return super().get_object(*args, **kwargs)

I am shure - this is the better solution, than send pk in url.

LoginRequiredMixin- default django mixin for GCBV DeteilView - django GCBV

More here: https://docs.djangoproject.com/en/4.1/topics/auth/default/#django.contrib.auth.mixins.LoginRequiredMixin

and

https://docs.djangoproject.com/en/4.1/ref/class-based-views/generic-display/#detailview

Back to Top