Django:如何使用参数重定向 [英] Django: How to redirect with arguments
本文介绍了Django:如何使用参数重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
提交表单后,我想重定向到传递一个flag = True的特定视图,以激活如下弹出窗口:
After submit a form, I want to redirect to an specific view passing one flag=True in order to activate a popup like:
def view1(request):
if request.method == 'POST':
form = Form(request.POST)
if form.is_valid():
form.save()
return redirect('new_view') # Here I need to send flag=True
else:
form = Form()
return render(request, 'template.html', {'form': form})
我该怎么做?
推荐答案
对于参数的含义尚不清楚,是否应该在查询字符串或视图的参数中.
It's not quite clear on what you mean by arguments if it should be in the query string or arguments to a view.
无论哪种方式,以下都是这两种解决方案;
Either way, below is both solutions;
重定向接受args和kwargs
redirect accepts args and kwargs
redirect('new_view', flag='show') # This is the argument of a view
或
redirect('{}?flag=True'.format(reverse('new_view'))
然后您可以像这样在视图中访问它
Then you can access it in the view like so
show_flag = bool(request.GET.get('flag', False))
这篇关于Django:如何使用参数重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文