我有一个Django表单,在验证后我想重定向到成功页面。我从" localhost / upload"开始,提交我的表单,如果验证我想转到localhost / upload / success。下面的代码可以工作,我只是希望能够传递一个带有一些变量的上下文字典来呈现给页面。我只能使用(在下面的views.py代码中进行哈希)
return render(request, 'results/success.html', context)
但是这会返回到localhost / upload,但是使用我的success.html模板 - 我想要上传/成功。
这是我的代码:
views.py:
@login_required(login_url='/')
def upload(request):
""" Upload panel or results.
"""
if request.method == 'POST':
paneluploadform = PanelUploadForm(request.POST, request.FILES)
if paneluploadform.is_valid():
paneluploadform.upload()
context = {'hi': 'helloasdgja'}
# return render(request, 'results/success.html', context)
return HttpResponseRedirect('success')
else:
# print(paneluploadform.errors.as_text)
pass
else:
paneluploadform = PanelUploadForm()
context = {'paneluploadform': paneluploadform}
return render(request, 'results/upload.html', context)
@login_required(login_url='/')
def success(request):
""" Success view
"""
return render(request, 'results/success.html')
urls.py:
from django.conf.urls import url
from .views import *
urlpatterns = [
url(r'^upload/$', upload),
url(r'^upload/success/', success)
]
我可以使用散列返回render()行来给出我的测试上下文,但这会返回" localhost:8000 / upload"
我想转到localhost:800 / upload / success,但是从上传视图获取信息并重定向到上传/成功。我可以将上下文arg传递给HttpResponseRedirect,还是可以解决这个问题?
答案 0 :(得分:0)
会话变量对于在页面/视图之间传递信息非常有用。
https://docs.djangoproject.com/en/dev/topics/http/sessions/
Ian Clelland在这里突出您的选择方面做得很好: https://stackoverflow.com/a/8931063/4928578