python - Django: how to transfer scattered data to a template? -


introduction

in django, when data want display on template included in 1 object, it's f**** easy. sum steps (that knows actually):

  1. you write right method object in model class
  2. you call method in view, passing result template
  3. you iterate on result in template for loop, display objects in table, example.

now, let's take more complex situation

  • let's data want display spread on different objects of different classes. need call many methods these data.
  • once call these different methods, got different variables (unsimilar objects, integers, list of strings, etc.)
  • nevertheless, still want pass template , display pretty table in end.

the problem is:

  • if you're passing raw objects containing data need template, unorganised , can't iterate on variables in clean way need display table.

the question is:

how (which structure) , (models? views?) should organize complex data before passing template?

my idea on this (which can totally wrong):

for each view need "spread data" pass template, create method (like viewxxx_organize_data()) in views.py, take raws objects , return data structure organized data me display table iterating on it.

about data structure choose, compared lists dictionaries

  • dictionaries have key it's cleaner call {{dict.a-key-name}} rather {{ tabl.3}} in template.
  • lists can sorted, when need sort date elements want display, dictionary not helpful, arghh, stuck again!

what think that? reading until there, , sharing on this!

with question entering in conceptual/architectural domain rather in "this particular view of data in project hard represent in template layer of django". try give birds view (when flying , not on ground) of problem , can decide yourself.

from first philosophy box in django template language documentation it's stated templates should have little program logic possible. indicates representation of data used in template should simple , totally adapted template trying build (this interpretation of it). approach indicates should have layer responsible intermediating representation of data (models or other sources) , data template needs achieve final representation want users see.

this layer can simple stay in view, in viewxxx_organize_data, or in other form respecting more complex/elaborated architecture (see dci or hexagonal).

in case start doing viewxxx_organize_data() use appropriate data structures template trying build, while keeping independence way obtain data (through models other services etc). can think of not using model objects directly in template , creating template specific objects represent view of data.

hope helps make decision. it's not concrete answer sure make decision , staying coherent trough app.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -