mercredi 13 août 2014

Modèle imbriqué dynamique Django - Stack Overflow


I have a base template called message_base which extends the main base template, and I need to show a dynamic menu in the message_base... Now I know that I can use template context processors but I was wondering if there is any way to restrict the data to be only accessible from the message_base and its child pages not the whole application, because I think sending data to all pages in every single request is kind of unnecessary.


Thanks




Sending data to all pages is not just unnecessary; it is hazardous. You end up with a situation that looks a lot like what would happen if you used global variables everywhere.


I'm not sure if there is a better way to do what you want to do, so I'm just going to tell you what we do.


Instead of extending base_template, I would add a context variable in there, say {{ message_area }}. Then, I would set that context variable to the output of render() on your message_base template. For example:


base_template.render(Context({..., 'message_area': message_base.render(...)}))

That keeps the data that only message_base should be concerned with localized to message_base.




Your message_base is not a separate page. It's a part of just one page that will be rendered by Django's template engine. message_base extends base template which means that it is a part of the same page which is built using the parent template base and the child template message_base. Your data is already available to the whole application. And once the template is rendered, only the HTML is sent back to the browser. It's not like data is sent back on each request and then the templates are rendered on the browser's side.



I have a base template called message_base which extends the main base template, and I need to show a dynamic menu in the message_base... Now I know that I can use template context processors but I was wondering if there is any way to restrict the data to be only accessible from the message_base and its child pages not the whole application, because I think sending data to all pages in every single request is kind of unnecessary.


Thanks



Sending data to all pages is not just unnecessary; it is hazardous. You end up with a situation that looks a lot like what would happen if you used global variables everywhere.


I'm not sure if there is a better way to do what you want to do, so I'm just going to tell you what we do.


Instead of extending base_template, I would add a context variable in there, say {{ message_area }}. Then, I would set that context variable to the output of render() on your message_base template. For example:


base_template.render(Context({..., 'message_area': message_base.render(...)}))

That keeps the data that only message_base should be concerned with localized to message_base.



Your message_base is not a separate page. It's a part of just one page that will be rendered by Django's template engine. message_base extends base template which means that it is a part of the same page which is built using the parent template base and the child template message_base. Your data is already available to the whole application. And once the template is rendered, only the HTML is sent back to the browser. It's not like data is sent back on each request and then the templates are rendered on the browser's side.


0 commentaires:

Enregistrer un commentaire