Home > Enterprise >  django not finding redirect page
django not finding redirect page

Time:02-20

hey guys i have the following codes, I'm trying to redirect the GoalUpdate view to GoalPageView , but I get the following Error:

Reverse for 'Goals.views.GoalPageView' with keyword arguments '{'kwargs': {'username': 'admin', 'goal_id': '1'}}' not found. 1 pattern(s) tried: ['people/(?P[^/] )/goals/(?P<goal_id>[^/] )/\Z']

My URLs

urlpatterns = [
    path('<username>/goals/',GoalsView,name='Goals'),
    path('<username>/goals/<goal_id>/',GoalPageView,name='Goal Page'),
    path('<username>/goals/<goal_id>/update/',GoalUpdate,name='Goal Update'),
                ]

My Views:

def GoalPageView(request,username,goal_id):

    some view code

    return render(request,'goal_page.html',context=context)



def GoalUpdate(request,username,goal_id):

    Some View Code
    
    return redirect(GoalPageView,kwargs={'username':username,'goal_id':goal_id})

CodePudding user response:

redirect does not take args and kwargs but uses positional and named parameters directly, so:

def GoalUpdate(request, username, goal_id):
    # some view code …
    return redirect('Goal Page', username=username, goal_id=goal_id)

Note: Functions are normally written in snake_case, not PascalCase, therefore it is advisable to rename your function to goal_update, not GoalUpdate.

CodePudding user response:

You have to use name of url, not the View itself. To add kwargs like you can use reverse_lazy:

return redirect(reverse_lazy('Goal Page', kwargs={'username':username, 'goal_id':goal_id}))

But I think Willem gave you simpler solution.

  • Related