嘿伙计们,我有以下代码,我正在尝试将 GoalUpdate View 重定向到 GoalPageView ,但出现以下错误:
未找到带有关键字参数 '{'kwargs': {'username': 'admin', 'goal_id': '1'}}' 的反向 'Goals.views.GoalPageView'。尝试了 1 个模式:['people/(?P[^/]+)/goals/(?P
我的网址
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'),
]
我的观点:
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})
最佳答案
redirect
不采用 args
和 kwargs
而是直接使用位置参数和命名参数,因此:
def GoalUpdate(request, username, goal_id):
# some view code …
return redirect('Goal Page', <strong>username=username, goal_id=goal_id</strong>)
Note: Functions are normally written in snake_case, not PascalCase, therefore it is advisable to rename your function to
goal_update
, not.GoalUpdate
关于Django 找不到重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71184189/