Refer to multiple Models in View/Template in Django

2024/9/28 3:44:01

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 Contactclass ListDashboardView(ListView):model = Contacttemplate_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.

Answer

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

class ListDashboardView(ListView):model = Contacttemplate_name = 'dashboard.html'def get_context_data(self, **kwargs):ctx = super(ListDashboardView, self).get_context_data(**kwargs)ctx['polls'] = Poll.objects.all()return ctx
https://en.xdnf.cn/q/71281.html

Related Q&A

Can I use a machine learning model as the objective function in an optimization problem?

I have a data set for which I use Sklearn Decision Tree regression machine learning package to build a model for prediction purposes. Subsequently, I am trying to utilize scipy.optimize package to solv…

How to store data like Freebase does?

I admit that this is basically a duplicate question of Use freebase data on local server? but I need more detailed answers than have already been given thereIve fallen absolutely in love with Freebase…

Django-celery : Passing request Object to worker

How can i pass django request object to celery worker. When try to pass the request object it throws a Error Cant Pickle Input ObjectsIt seems that celery serialize any arguments passed to worker. I tr…

How to get ROC curve for decision tree?

I am trying to find ROC curve and AUROC curve for decision tree. My code was something likeclf.fit(x,y) y_score = clf.fit(x,y).decision_function(test[col]) pred = clf.predict_proba(test[col]) print(skl…

pandas - stacked bar chart with timeseries data

Im trying to create a stacked bar chart in pandas using time series data:DATE TYPE VOL0 2010-01-01 Heavy 932.6129031 2010-01-01 Light 370.6129032 2010-01-01 Medium 569.4516133 …

Get element at position with Selenium

Is it possible to either run or get the same functionality provided by document.elementFromPoint using a Selenium webdriver?

Facing obstacle to install pyodbc and pymssql in ubuntu 16.04

I want to install pyodbc for connection mssql server using sqlalchemy I am googling and tried in several ways like pip install pyodbcFollowed this link Pyodbc installation error on Ubuntu 16.04 with S…

Cross entropy loss suddenly increases to infinity

I am attempting to replicate an deep convolution neural network from a research paper. I have implemented the architecture, but after 10 epochs, my cross entropy loss suddenly increases to infinity. Th…

Converting each element of a list to tuple

to convert each element of list to tuple like following : l = [abc,xyz,test]convert to tuple list: newl = [(abc,),(xyz,),(test,)]Actually I have dict with keys like this so for searching purpose I need…

Python, Zeep response to pandas

I am tryng to conenct to a SOAP webservice and use pandas to put in on a table.Zeep give me this list:[{ssPeca: 103,ssQtd: 1,ssUn: un }, {ssPeca: 291A,ssQtd: 8,ssUn: un }, {ssPeca: 406B,ssQtd: 8,ssUn: …