首页 > 解决方案 > 通过view1呈现的HTML将参数从视图函数传递到另一个而不更改URL?

问题描述

我在 django 中有 2 个视图函数:

def view1(request):
    dict_ex = Mod.objects.all()
    ............... (modifying and processing the dict_ex variable)
    return render(request, 'view1.html', {"dd": dict_ex})

然后我在 view1.html 中有这些行:

{% for k, v in dd.items %}
    Name: {{ v }}
    <a class="btn btn-primary" href="{% url 'view2' k.id %}">Choose this </a>
{% endfor %}

然后我的 view2 需要修改后的 dict_ex 来获取所需的其他值:

def view2(request):
    dict_ex = Mod.objects.all()
    ............... (modifying and processing the dict_ex variable)
    ....... (obtaining var1 from different operations on dict_ex)
    return render(request, 'view2.html', {"v": var1})

有什么方法可以通过 view1.html 将修改后的 dict_ex 从 view1 传递到 view2 函数?

我也不想在 view2 中重复这些代码行

dict_ex = Mod.objects.all()
............... (modifying and processing the dict_ex variable)

有没有办法像这样访问 view2 函数:

view2(request, dict_ex)

不改变网址?

我的 URL_PATTERNS 中有这些:

path('view_one/', view1, name='view1'),
path('view_two/<int:id>', view2, name='view2'),

标签: pythondjango

解决方案


推荐阅读