Django: Using same object how to show 2 different results in django template?

2024/10/12 5:50:09

Using the same object how to SHOW 2 different results using django template ?

In one page there are two div's, it should show different information using the same object.

INPUT

object data has follows

[{"Google": [{"Rating": 1,"Website": {"id": "1","Name": "googleplus"}},{"Rating": 2,"Website": {"id": "1","Name": "googleplus"}},{"Rating": 1,"Website": {"id": "2","Name": "googlemap"}}]},{"Facebook": [{"Rating": 1,"Website": {"id": "1","Name": "facebookplus"}},{"Rating": 2,"Website": {"id": "1","Name": "facebookplus"}},{"Rating": 1,"Website": {"id": "2","Name": "facebookmap"}}]}
]

DESIRED OUTPUT

DIV 1 (website names should be unique values)

DIV 2 (should display all website names)

main div start

loop 1

Google  ------  DIV 1 (googleplus, googlemap) ------ DIV 2 (googleplus, googleplus, googlemap) 

loop 2

Facebook  ------  DIV 1 (facebookplus, facebookmap) ------ DIV 2 (facebookplus, facebookplus, facebookmap) 

main div end

Answer

Template is used only to display given data. If you want to manipulate data, you have to do this in view. It'll be a lot easier, as you can use normal python syntax.

You can use something like:

all_data = <object_name>.objects.all()
unique_data = list(set(all_data))

UPDATE

If you want to do this on frontend, I would recommend using Lodash library.

But you can of course do it in plain JavaScript:

var all_data = ...;
var unique_data = [];
for (i = 0; i < all_data.length; i++) {if (unique_data.indexOf(all_data[i]) < 0) unique_data.push(all_data[i]);
}
https://en.xdnf.cn/q/118234.html

Related Q&A

Override attribute access precedence having a data descriptor

I have a bunch of instances of a MongoEngine model. And the profiler shows that a lot of time is spent in __get__ method of MongoEngine model fields:ncalls tottime percall cumtime percall filename:…

Understanding pythons reverse slice ( [::-1] )

I always thought that omitting arguments in the python slice operation would result into:start = 0 end = len(lst) step = 1That holds true if the step is positive, but as soon as the step is negative, l…

How to print list elements (which are also lists) in separated lines in Python

Ive checked the post and answers on the SO post Printing list elements on separated lines in Python, while I think my problem is a different one.What I want is to transform:lsts = [[1], [1, 1], [1, 2, …

Python3 threading, trying to ping multiple IPs/test port simultaineously

Full (non-working) code belowFull (working, w/o threading) code here: http://pastebin.com/KUYzNtT2Ive written a small script that does the following:Pull network information from a database Ping each I…

How to print a list of numbers without square brackets?

Im generating a list of random digits, but Im struggling to figure out how to output the digits in a single row without the square brackets?import random def generateAnswer(answerList):listofDigits =…

How to save plotly offline by running my script

I am using below code in my jupyter notebook.import pandas as pd import numpy as np %matplotlib inlinefrom plotly import __version__ from plotly.offline import download_plotlyjs, init_notebook_mode, pl…

Same output of the Keras model

I have a Keras model for predicting moves in the game. I have an input shape of (160,120 ,1). I have the following model with an output of 9 nodes:from keras.models import Sequential from keras.layers.…

Extract common element from 2 tuples python [duplicate]

This question already has answers here:Find intersection of two nested lists?(21 answers)Is there a way to get the difference and intersection of tuples or lists in Python? [duplicate](1 answer)Close…

How to make an integer index row?

I have a DataFrame: +-----+--------+---------+ | usn|log_type|item_code| +-----+--------+---------+ | 0| 11| I0938| | 916| 19| I0009| | 916| 51| I1097| | 916| 19| …

Matplotlib plt.plot with enumerate not working

import numpy as np import matplotlib.pyplot as plt array = np.array([[1,2,3,4,5,6],[10,20,30,40,50,60],[3,4,5,6,7,8],[100,200,300,400,500,600]])def plot(list):fig = plt.figure()ax = fig.add_subplot(11…