I have the following view:
我有以下观点:
def copy_group(request):
copy = request.GET.get('copy','')
if copy:
#do my copy process
context = {'view':'copy-view'}
return render(request, 'groups/copy-view.html', context)
This invoked with the following url mysite.com/groups/?copy=1
使用以下url mysite.com/groups/?copy=1调用
The problem is that if I refresh the page, my process keeps copying over and over.
问题是,如果我刷新页面,我的进程就会不断地复制。
How can I remove the ge parameter so the url returns after it copies: mysite.com/groups/
如何删除ge参数,以便url在它复制之后返回:mysite.com/groups/
I tried inserting this in my view code:
我尝试把这个插入到我的视图代码中:
request.GET.pop('copy')
But I get the error: This QueryDict instance is immutable
但是我得到了一个错误:这个QueryDict实例是不可变的
2 个解决方案
#1
1
If I understand it well, once you've done the copy you want the page to be redirected so no more copy will be made. Do like this then:
如果我理解的很好,一旦你完成了复制,你希望页面被重定向,这样就不会再复制了。这样做:
...
if copy:
the actual copy and then...
return HttpResponseRedirect(redirect_to='/the-path-without-copy-parameter/')
...
Then you can improve your code:
然后你可以改进你的代码:
- add copy as an actual route parameter, def copy_group(request, copy=None)
- 添加copy作为实际的路由参数def copy_group(request, copy=None)
- generate the path in redirect_to instead of hardcoding it
- 在redirect_to中生成路径,而不是硬编码它
- Out of topic: add a functional test for it if there is none :)
- 脱离主题:如果没有的话,为它添加一个功能测试:)
BTW, I'm not sure why you use a Get parameter and not just a different URL for it??
顺便说一句,我不知道为什么要使用Get参数而不是不同的URL ?
#2
0
I ended up doing a redirect instead. That removes the get parameter.
结果我重定向了。这将删除get参数。
#1
1
If I understand it well, once you've done the copy you want the page to be redirected so no more copy will be made. Do like this then:
如果我理解的很好,一旦你完成了复制,你希望页面被重定向,这样就不会再复制了。这样做:
...
if copy:
the actual copy and then...
return HttpResponseRedirect(redirect_to='/the-path-without-copy-parameter/')
...
Then you can improve your code:
然后你可以改进你的代码:
- add copy as an actual route parameter, def copy_group(request, copy=None)
- 添加copy作为实际的路由参数def copy_group(request, copy=None)
- generate the path in redirect_to instead of hardcoding it
- 在redirect_to中生成路径,而不是硬编码它
- Out of topic: add a functional test for it if there is none :)
- 脱离主题:如果没有的话,为它添加一个功能测试:)
BTW, I'm not sure why you use a Get parameter and not just a different URL for it??
顺便说一句,我不知道为什么要使用Get参数而不是不同的URL ?
#2
0
I ended up doing a redirect instead. That removes the get parameter.
结果我重定向了。这将删除get参数。