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>
adding urls.py
urlpatterns = [
path('',views.home, name='home'),
path('login/',views.loginp, name='login'),
path('profile/<str:ck>/',views.profile, name='profile'),
path('Edit_profile/<str:ck>/',views.Edit, name='Edit'),
path('logout/',views.logoutp, name='logout'),
path('register/',views.register, name='register'),
]
page where my button is located
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<input type ="submit" value="Save">
</form>
<a href="{% url 'profile' ?? %}"><button>Exit</button></a>
CodePudding user response:
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>
CodePudding user response:
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
and
https://docs.djangoproject.com/en/4.1/ref/class-based-views/generic-display/#detailview