gpt4 book ai didi

python - Django:在传递的 View 中使用 HttpResponseRedirect(reverse()) 访问 kwargs

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:25 25 4
gpt4 key购买 nike

我正在尝试在成功 View 中访问通过 HttpResponseRedirect() 传递的 kwargs。键存在于 kwarg 字典中,但值不存在。这是为什么?

浏览次数:

class UploadView(View):

paneluploadform = PanelUploadForm

def get(self, request, *args, **kwargs):

paneluploadform = self.paneluploadform()
context = {'paneluploadform': paneluploadform}
return render(request, 'results/upload.html', context)


def post(self, request, *args, **kwargs):

paneluploadform = self.paneluploadform(request.POST, request.FILES)

if paneluploadform.is_valid():
panel_name = paneluploadform.upload()
return HttpResponseRedirect(
reverse('results:success',
kwargs={'panel_name': panel_name})
)


context = {'paneluploadform': paneluploadform}
return render(request, 'results/upload.html', context)


def success(request, *args, **kwargs):

return render(request, 'results/success.html')

url.py:

urlpatterns = [
url(r'^upload/$', UploadView.as_view(), name='upload'),
url(r'^upload/successful-(?P<panel_name>)', success, name='success')
]

在我的 success.py View 中,当我 print(kwargs) 时,输出为 {'panel_name': ''}

但我的网址是/localhost/upload/successful-panelnameientered

为什么不 kwargs ={'panel_name': 'panelnameientered'}

最佳答案

我不完全确定,但我认为您应该使用re_path,并且您没有设置模式来匹配successful-之后的内容。所以它应该是这样的:

re_path(r'^upload/successful-(?P<panel_name>[a-z]+)', success)

关于python - Django:在传递的 View 中使用 HttpResponseRedirect(reverse()) 访问 kwargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com