Django: rendering more than one view in a template

I am newbie in Django and didn't know how to render more than one view in a template. This is my case, I have a template (main.html) and I have 2 sections: a home page and one at the top where the user data (messages, log off, etc ...) will be displayed.

My question is this, can I make 2 partial views (_index.html and _userdata.html) and render them separately and display them in the template. In the same way that the "include" php or ASP.NET MVC partial views. Or spend the model or models to the template with all the information.

As I haven't explained well, an example of real life would be, Amazon or any online book store. Where you can see the books or products on the right side and have your shopping cart with your products shown. How would that Django? Have an HTML template and view the 2 models you send or you can do 2 separate views and are rendered in the same HTML template?

Upvotes: 0

Views: 391

Answers (1)

cdvv7788
cdvv7788

Reputation: 2099

Those things are bound to the user, so you could get them in your parent templates.

Example:

parent.html

{{ request.user.get_something }}

define get_something(self) in the user model (your items in the cart, etc).

You can also do something like:

{% include 'elements/my_something.html' %}

Upvotes: 1

Related Questions