Using multiple views in one page

Hello. How can I use multiple views in one pages(In one html file)

1 Like

You don’t.

A view receives a request and returns a response.

In the common case, a view can render a template using a context to build that response.

The view can call multiple functions to obtain data to add to the context.

The template can include multiple templates to render the response.

But overall, it’s just one view being called for the request.

1 Like

you can do this with django and htmx. you can have a view render a template but within that template you can reference many other views via htmx. you can refer bugbytes channel on youtube for the same.

While this will work exactly as you’ve described, it’s still not “one template / multiple views”. Each one of those separate views are going to render their own HTML to augment the existing page. You are not building a page from a single template with content from multiple views.

yes, a good compromise (or a better compromise than) between one view per template or learn a separate frontend like angular, react, vue; then convert your django to rest api with DRF and deploy and maintain them them separately.

Hello Ken
So the question now is:
Can one have one view for extend templates?
Thanks

I’m sorry, I don’t think I understand what you’re asking here.

A view receives a request. The view can render one or more templates. Those templates can contain extends and include tags. But, the view returns one single response. The view has complete control over the content of that response, but it’s still one response.

You mean the view can render one or more templates but with only one response? What about if I get query sets from multiple tables with the same view? How do I render that?

You can have different sections in the template that render the two different querysets.

The query sets will be kept in memory as objects to use when needed? Then with HTML, CSS…we can write the template with different query sets results has context?

For the duration of that view, yes.

Yes, you can pass whatever data you want to the template rendering engine through the context.

1 Like

One last question, how do I know the duration of the view?

A view accepts a request and returns a response.

It is started when it’s called and goes away when it returns that response. It’s like any other Python function.

Ok thanks Ken, I will keep trying :v:

Hello ken …

I have a question please help me if u can …

I wrote a view that shows the categories on the navbar of index page only. But i wanna to be shown on all my pages.
What can i do ?
Should i repeat all categories’s function in all views ? Or there is another solution

I’d suggest you open up a new discussion topic for this.

I dont know how to do that :cry:

Click on the “+ New Topic” button at the top right of the forum home page. (It shows up on many other pages as well.)

I think i did now …please check