将数据从帖子表格传递到归档的视图
我最近开始使用Django,并设法创建了两个视图,一个要提交表格,另一个提交表单以返回档案响应,它们正常工作。
现在,我需要将两者集成在一起,当客户端提交表单时,我想使用上一张表格中提交的字段重定向到另一个视图。我该怎么做?
这是我的表单视图:
def submitForm(request):
if 'report' in request.POST:
date_start = request.POST.get('date_start')
date_end = request.POST.get('date_end')
state = request.POST.get('state')
return render(request, 'comissao.html')
这是我的观点,它可以创建一个PDF文件
def createPdf(request):
date_start = '20220301'
date_end = '20220331'
state = 'G00471'
body = "some html"
options = { 'quiet': '' }
pdfkit.from_string(body, options=options)
file = open('file.pdf', 'rb')
return FileResponse(file)
,如您所见,我需要在我的第一个视图中传递的信息,以便在第二视图中使用,我尝试了这样的东西,但是我想我误认为概念,
<代码>返回反向('pdf',kwargs = {'state':state,'date_start':date_start,'date_end':date_end':date_end})>
I recently started using Django and I managed to create two views, one to submit a form and another to return a FileResponse, separately, they work fine.
Now, I need to integrate both, when the client submit the form, I want to redirect to the another view using the fields submitted at the previous form. How can I do that?
Here is my form view:
def submitForm(request):
if 'report' in request.POST:
date_start = request.POST.get('date_start')
date_end = request.POST.get('date_end')
state = request.POST.get('state')
return render(request, 'comissao.html')
Here is my view that creates a pdf file
def createPdf(request):
date_start = '20220301'
date_end = '20220331'
state = 'G00471'
body = "some html"
options = { 'quiet': '' }
pdfkit.from_string(body, options=options)
file = open('file.pdf', 'rb')
return FileResponse(file)
As you can see, I need the information passed at my first view, to use at second view, I tried something like this, but I think I'm mistaking the concept,
return reverse('pdf', kwargs={'state':state, 'date_start':date_start, 'date_end':date_end})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反向()
仅返回URL,但我认为您宁愿将redirect()
对此视图进行。对于传递参数,您有几个选项:
,让我们使用获取参数,我建议:
然后,在您的PDF视图中,您需要解析get参数:
请注意,您可能还需要将日期转换为字符串和字符串和返回查询,我在这里忽略了查询。
reverse()
only returns the URL, but I think you rather want to do aredirect()
to that view.For passing the parameters you have several options:
Let's use GET parameters, which I would suggest:
Then in your PDF view you need to parse the GET parameters:
Note that you may also need to convert your dates into string and back for the query, which I ignored here.