[SOLVED] reverse_lazy not redirecting to the correct link (adding a suffix)

Issue

This Content is from Stack Overflow. Question asked by Ayat Rahman

so this is my class based function

class AddCommentView(CreateView):
model = Comment

form_class = CommentForm
template_name = 'app/add_comment.html'
def form_valid(self, form):
list_obj = List.objects.get(slug = self.kwargs['slug'])
        form.instance.list = list_obj
return super().form_valid(form)

success_url = reverse_lazy("app:list_detail", kwargs={'slug': List.slug})

once the comment is submitted I wanna go back to detailview page which is

http://127.0.0.1:8000/home/list/best-bangla-art-films2022-09-09-0505595309260000/

but its taking me to

http://127.0.0.1:8000/home/list/best-bangla-art-films2022-09-09-0505595309260000/comment/

any way i can solve this?

in my urls

(I have excluded irrelevant paths)

app_name = "app"
urlpatterns = [
path('', views.HomeView.as_view(),name='home'),
path('list/<slug:slug>/', TheirDetailView.as_view(),name='list_detail'),
path('list/<slug:slug>/comment/',AddCommentView.as_view(),name="add_comment"),
]



Solution

Try to change success_url = reverse_lazy("app:list_detail", kwargs={'slug': List.slug}) to success_url = reverse_lazy("app:list_detail", kwargs={'slug': list_obj.slug}).
You should use the variable name, not the Model


This Question was asked in StackOverflow by Ayat Rahman and Answered by Giorgio Scarso It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?