Django将对象从视图传递到下一个进行处理 [英] Django pass object from view to next for processing

查看:79
本文介绍了Django将对象从视图传递到下一个进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有2个视图,则第一个使用modelform来获取用户输入的信息(出生日期,姓名,电话号码等),第二个使用此信息来创建表格。

If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table.

如何将第一个视图中创建的对象传递给下一个视图,以便可以在第二个视图的模板中使用它

How would you pass the created object in the first view to the next view so you can use it in the second view's template

I' d感谢您可以分享的任何帮助

I'd appreciate any help you can share

推荐答案

一种方法是将对象放入第一个视图的会话中,您可以然后从第二个视图的request.session中检索。

One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view.

def first_view(request):
    my_thing = {'foo' : 'bar'}
    request.session['my_thing'] = my_thing
    return render(request, 'some_template.html')

def second_view(request):
    my_thing = request.session.get('my_thing', None)
    return render(request, 'some_other_template.html', {'my_thing' : my_thing})

这篇关于Django将对象从视图传递到下一个进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆