0

I have a simple UpdateView which updates instances of my Profile model.

class ProfileUpdateView(UpdateView):

    model = Profile
    fields = ['first_name', 'last_name']

It's working fine both GET and POST, but since the GET uses an url parameter, I want also the redirect_url to redirect to the updated profile.

My current urls.py:

from profiles.views import ProfileUpdateView

urlpatterns = [
    path('self/<slug:slug>', ProfileUpdateView.as_view(success_url='/'), name='self_profile')
]

It is currently redirecting to the main url, but I don't figure out how to make the redirect_url parameter redirect to the profile modified on the handled POST.

Let's say I do http://localhost:8000/profiles/self/demo, (GET), then I modify my first_name and submit the request. I want the redirection to go to /profiles/self/demo again.

I know I could override def post() on the view and achieve it, but since I only want to deal with the redirection I wanted to know if exists any other elegant solution.

EDIT: Additional info: the slug url parameter will always be request.user in this case (since I use other view (a DetailView) to see other users' profiles).

Thanks!!

1 Answer 1

1

Override the get_success_url(...) method as

class ProfileUpdateView(UpdateView):
    model = Profile
    fields = ['first_name', 'last_name']

    def get_success_url(self):
        return '/profiles/self/demo'

Hence, you don't need to specify the success_url attribute in the urls.py

from profiles.views import ProfileUpdateView

urlpatterns = [
    path('self/<slug:slug>', ProfileUpdateView.as_view(), name='self_profile')
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.