One Template multiple views

I have two models: Employee and Department. I have created separate views functions emp_view and dept_view.

emp_view renders the employee employee.html and dept_view renders department.html
Both of these web pages work fine by themselves and displays the respective tables with records

But, I want to display both the tables in my index.html via the two views. Simply doing two separate include template tags does not display the display any records for both tables.

What would be best solution to display both the tables while retaining separate views and templates?

Thanks in advance!

You create a view that queries both models, and renders a template to display them both.

Hi Ken,

Thanks for your response. That would mean just one view and and one template, correct. My original purpose was to ensure modularity so, each view renders a separate template showing one table and somehow display both the templates dynamically in index.html

Each view generates one page. You do not combine views on a page.

However, a template, by virtue of the {% include %} tag, can render multiple templates on a page.

Can something like this be refactored out to reduce duplication? Sure.

However, in practice, there are a lot of situations where “modularity” results in more work than replication, especially in the simple / common case.

Yes, you could create multiple templates here. You could have your employee page (with its template) include an employee_data template. You could then have your index page (with its template) also include that employee_data template. Likewise, you can do something similar with the departments. Both the department page template and the index page template could include the department_data template. So the net effect is that you’re increasing the number of files by two, and reducing the duplication of the template definitions for the display of this data.

And there are some cases where it’s probably worthwhile to do this - I’m not saying it’s not a good idea. Just that there are a number of situations where it’s not much of a benefit.

Thanks for clarifying this, Ken. I tried rendering department and employee templates on a single page through the include function. It simply seems to render the static HTML content from these templates without running the associated views which displays the necessary tables dynamically. Looks like it is not possible in Django.
As you alluded the solution would be to use a single view that sources both the models and render a single template.

Thanks for all your help!

Correct. Templates don’t run views.

Views render templates.

Templates are just text data being processed by a rendering engine.

Again, you can refactor the views out into separate functions - but also again, that’s not always worthwhile.