如何在Django中将列表从一个视图传递到另一个视图? [英] How do I pass a list from one view to another in Django?

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

问题描述

我一直在寻找StackOverflow,但没有找到适合我的答案.我对Python和Django比较陌生,所以也许我在想错了.

I've been scouring StackOverflow, but I haven't found an answer to this that works for me. I am relatively new to Python and Django, so maybe I'm thinking about it wrong.

举一个简单的例子,设想两个具有不同关联URL的视图.这不应该是完美的代码.我只是想弄清楚如何从视图1到视图2中获取可变长度的项目列表.我看不到通过URL进行操作的方法,因为该列表可能会变长.这不应该很容易做到吗?

To make a simple example, imagine two views with different associated URLs. This is not supposed to be perfect code. I'm just trying to figure out how to get a variable-length list of items from view 1 into view 2. I don't see a way to do it via the URL because the list may be variably long. Shouldn't this be extremely easy to do?

def view2(request, list_to_process):

     use list_to_process to manufacture formset (e.g. make a formset with one entry for each item in the list)
     return render(request, 'Project/template2.html', {'formset': formset})

def view1(request):

    if request.method == "POST":
        if form.is_valid():
            result = form.cleaned_data
            list_to_process = []
            for item in result:
                list_to_process.append(item)
            *WHAT CODE DO I USE HERE TO CALL VIEW2 AND SEND IT list_to_process AS AN ARGUMENT OR REQUEST ADDITION?*
    else:
        formset = formsettype()
        helper = AssayHelper() (defined elsewhere)
        helper.add_input(Submit("submit", "Submit")
        return render(request, 'Project/template1.html', {'formset': formset, 'helper': helper})

有人可以帮忙吗?谢谢.

Can someone please help? Thanks.

推荐答案

这正是会话的目的.在视图1中:

That is exactly what the session is for. In view 1:

request.session['list'] = list_to_process

在视图2中:

list_to_process = request.session['list']

这篇关于如何在Django中将列表从一个视图传递到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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