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

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

问题描述

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

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 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天全站免登陆