请参阅Django中View / Template中的多个模型 [英] Refer to multiple Models in View/Template in Django

查看:180
本文介绍了请参阅Django中View / Template中的多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python / Django进行第一步,并在一个Django项目中使用多个Django应用程序编写了一个示例应用程序。现在我添加了另一个名为仪表板的应用程序,我想从不同的应用程序显示数据。目前,我仍然使用这个简单的基于类的通用视图,其中显示了我的小联系人的条目 - 仪表板上的应用程序:



views.py:

  from django.views.generic import ListView 
from contacts.models import联系人

class ListDashboardView( ListView):
model =联系
template_name ='dashboard.html'

urls.py:

  url(r'^ $',dashboard.views.ListDashboardView.as_view(),
name ='dashboard-list',),

在dashboard.html中,我做:

 < ul> 
{%for object in object_list%}
< li class =contact> {{contact}}< / li>
{%endfor%}
< / ul>

有人可以向初学者解释如何在我的模板中访问多个模型?我不仅要显示我的联系人应用程序中的联系人,还可以显示其他应用程序的数据,例如我的库存应用程序和第三个应用程序。



知道,我必须导入它:

  from inventory.models import资产
来自polls.models import Poll

但是,使用视图将所有这些数据传递给单个模板需要做些什么?如何访问我的模板中的数据?



解决方案可能在 Django将多个模型传递给一个模板,但是我必须承认我不太明白。

解决方案

您需要覆盖 get_context_data 方法,并在上下文中传递任何您想要的内容:

  class ListDashboardView(ListView):
model = Contact
template_name ='dashboard.html'

def get_context_data self,** kwargs)
ctx = super(ListDashboardView,self).get_context_data(** kwargs)
ctx ['polls'] = Poll.objects.all()
return ctx


I'm making my first steps with Python/Django and wrote an example application with multiple Django apps in one Django project. Now I added another app called "dashboard" where I'd like to display data from different apps. At the moment I still use this simple class-based generic view, which shows the entries of my little contacts-App on the dashboard:

views.py:

from django.views.generic import ListView
from contacts.models import Contact

class ListDashboardView(ListView):
     model = Contact
     template_name = 'dashboard.html'

urls.py:

url(r'^$', dashboard.views.ListDashboardView.as_view(),
    name='dashboard-list',),

In dashboard.html I do:

<ul>
  {% for contact in object_list %}
    <li class="contact">{{ contact }}</li>
  {% endfor %}
</ul>

Can anybody explain to a beginner how to access multiple models in my template? I'd like to show not only the contacts from my 'contacts' app but also data from other apps such as my 'inventory' app and a third app.

I know, I have to import it:

from inventory.models import Asset
from polls.models import Poll

But what has to be done to pass all this data to my single template using a view? And how can I access that data in my template?

The solution may be in Django Pass Multiple Models to one Template but I must confess that I don't really understand it.

解决方案

You need to override the get_context_data method and pass whatever you want to in context:

class ListDashboardView(ListView):
    model = Contact
    template_name = 'dashboard.html'

    def get_context_data(self, **kwargs):
        ctx = super(ListDashboardView, self).get_context_data(**kwargs)
        ctx['polls'] = Poll.objects.all()
        return ctx

这篇关于请参阅Django中View / Template中的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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