开发者

Calling small app in template

开发者 https://www.devze.com 2022-12-12 06:08 出处:网络
Lets say I have a website with a small application that lists the 10 newest members, or something dynamic like that. I want this to view on every page, perhaps in a sidebar. How would i go about doing

Lets say I have a website with a small application that lists the 10 newest members, or something dynamic like that. I want this to view on every page, perhaps in a sidebar. How would i go about doing this.

My thoughts on the matter. I might not get the whole django thing just yet, but when I have a url like /foo/ calling a view bar - but what info do I have to send to the template from this view. Does every view have to send the info to the template (just so I can view my app) or is there someway to call this from the template instead.

I have tried to read through the documentation, but its seems I just can't understand this开发者_开发知识库.


The usual way to provide '10 newest members' type of information from other apps is via a template tag. See this article by James Bennett on best practices (although note it's a bit out of date, as it was written before the inclusion_tag and simple_tag shortcuts were available).


  1. Create a template tag that you can call on the page
  2. Create a context processor and inject extra context variables onto each pageload

I'm sure there are other ways of doing this but those are probably the most logical two. The first gives you more power and will waste less processing time (for pages where you don't want to display the data) but a context processor is much more simple to write (you don't have to bend over backwards to please the template_tag gods).

Both are valuable things to know so there you go. Go and learn!


"Does every view have to send the info to the template (just so I can view my app)"

Yes.

"Is there someway to call this from the template instead."

No.

Your views are just functions. Functions can call other functions. That's ordinary good design. You can still do ordinary good design in Django.

You do have the ability to provide a "context". This is still done in the views to provide additional "context" for the templates. See http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors for writing your own context processor.

Nothing (well almost nothing) is done in the template except render the objects provided by the view into HTML (or XML).


If you have a page that is an amalgamation of stuff from many small apps, then you have two tiers of apps.

  • Independent Apps.

  • Composite Apps that depend on Composite or Independent Apps.

Your composite app can call other app view functions to gather data.

Your composite app template can include other app template elements to present that data.

You have all the power of Python to decompose the independent apps into "data production" functions, view functions, template components and final page templates.

An independent app Page will use a view function and a template. The view function will use the data production functions. The template will use template components.

Decomposition still works, even in Django.

0

精彩评论

暂无评论...
验证码 换一张
取 消