返回多个变量Django的渲染

发布于 2025-02-13 05:39:52 字数 674 浏览 1 评论 0 原文

我在 views.pys.py 中定义 home 请求,

db=client.inventory_data
def home(request):
    collection_data_1 = db['orders']
    mydata = list(collection_data.find())
    return render(request,'home.html',{'mydata': mydata})

上面的功能正常工作,但是当我尝试返回另一个列表时, 不工作。

def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())
    return render(request,'home.html',{'mydata': mydata},{'product_data':product_data})

我想返回两个列表,我们该如何实现?寻找善良的帮助。

I define home request in views.py,

db=client.inventory_data
def home(request):
    collection_data_1 = db['orders']
    mydata = list(collection_data.find())
    return render(request,'home.html',{'mydata': mydata})

The above function works fine but when I try to return one more list, it does not work.

def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())
    return render(request,'home.html',{'mydata': mydata},{'product_data':product_data})

I want to return both the list, how can we achieve this? looking for kind help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自控 2025-02-20 05:39:52

您只需将两个字典组合到一个: {'mydata':mydata,'product_data':product_data}

def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())

    return render(request,'home.html',{'mydata': mydata, 'product_data': product_data})

当您分别通过两个字典时它不起作用的原因是因为渲染接受上下文(字典)为第三个参数, content_type (a String)为第四参数,因此,当您通过两个字典中传递时,您将词典以 content_type 传递。

如果有帮助,这是您最初用注释的变量名称所拥有的:

render(
   request=request,
   template_name='home.html',
   context={'mydata':mydata},
   content_type={'product_data':product_data},
)

这就是您现在拥有的:

render(
   request=request,
   template_name='home.html',
   context={'mydata': mydata, 'product_data': product_data}
)

You can simply combine the two dictionaries into one: {'mydata': mydata, 'product_data': product_data}

def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())

    return render(request,'home.html',{'mydata': mydata, 'product_data': product_data})

The reason that it didn't work when you passed in the two dictionaries separately is because render accepts context (a dictionary) as the third argument and content_type (a string) as the fourth argument, so when you passed in two dictionaries, you were passing in a dictionary as the content_type.

If it helps, here's what you originally had with the variable names annotated:

render(
   request=request,
   template_name='home.html',
   context={'mydata':mydata},
   content_type={'product_data':product_data},
)

And here's what you have now:

render(
   request=request,
   template_name='home.html',
   context={'mydata': mydata, 'product_data': product_data}
)
自由如风 2025-02-20 05:39:52
def home(request):
    # ...
    return render(request,'home.html',{'mydata': mydata, 'product_data':product_data})

def home(request):
    # ...
    return render(request,'home.html',{'mydata': mydata, 'product_data':product_data})

-黛色若梦 2025-02-20 05:39:52
def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())
    context = {
     'mydata': mydata,
     'product_data': product_data
    }
    return render(request,'home.html', context=context)
def home(request):
    collection_data_1 = db['orders']
    collection_data_2 = db['product']
    mydata = list(collection_data_1.find())
    product_data = list(collection_data_2.find())
    context = {
     'mydata': mydata,
     'product_data': product_data
    }
    return render(request,'home.html', context=context)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文