Am 03.11.2008 um 02:33 schrieb ilmarik:

> It's just simple amazing that I waste another night to figure out how
> to glue two and more apps together inside one template.
> I read about custom tags and I thing it isn't (or at least it
> shouldn't be) the way of doing it. too much fuss imho.
>
> Example:
> I have home page with menu on one side, news on other side, greetings
> article inside, footer menu on bottom and a survey somewere else.
> everything on one page of course.
>
> Walking with the idea of decoupled apps, I should create News app,
> Survey app, Menu app and so on...

There are several ways to get data from apps into templates:

Views:
---------
With views you have the ability to tailor the data you want to display  
to the needs of a specific page.
Views are not necessarily coupled to apps;
most apps have them, but then the views deal with the pages that are  
tied to the app.
Say, if you have a news app, it's most likely that the app contains  
some views to display the news on some specific pages like 'mysite/ 
news/', 'mysite/news/2008/11/' , 'mysite/news/2008/11/03/super-duper- 
great-news/'.
If you would like to have the news displayed on other not-tied-to-the- 
news-app pages like your homepage, you wouldn't do this in the  
views.py file of the news app but in a more global file like the  
views.py in your project directory (at least this is how I do it...).
In this file, you could wirte a view that gathers all the data you  
want to display on your home page, for example like this:

def index(request):

        news = News.objects.all()
        surveys = Survey.objects.all()
        menu_items = MenuItem.objects.all()

        return render_to_response('home.html',
                {
                'news': news,
                'surveys': surveys,
                'menu_items': menu_items,                               
                },
        context_instance=RequestContext(request))

So views are a good thing if it comes to specific pages, but there are  
better ways if you have something you want to display on every page.

Custom Tags:
-------------------
Custom tags are often tied to apps as well, wich makes sense.
With custom tags, you can filter the app's data and provide this  
filtered data directly to the template, without wire up some views for  
it, and in the same step add functionality to the template language.
You can do some other pretty sophisticated things with custom tags  
besides of just pulling data...
Their main use case is in my oppinion to seperate business logic from  
presentation logic.
With custom tags, the programmer can provide some special  
functionality for the template author.
For example you could write a custom tag for your news app that is  
used in the template like this:

{% get_news 4 1 as newslist %},

which would then be 'give me the four latest news items, but only if  
they are not older then one month, and make them available in the  
template as newslist'.

So you could use custom tags when you have some things that you want  
to display on every page, but if you want to do only this, then you  
are right:
It's a little bit to much fuss for such a simple task, custom tags can  
be used for much more complex things.


Context processors:
---------------------------
I think context processors the best way when it comes to just  
displaying some data on every single of your projects' pages.
They are easy to write, and they do just one thing:
Add some data to the context which gets passed to all templates.

A (simple) example would be:

def get_news(request):
        latest_news = News.objects.all()[:4]
        return {'lates_news': latest_news}

And voilĂ , from now on 'latest_news' is available in every temlate,  
and you can do things like this:

{% for news in latest_news %}
Do some stuff with your news
{% endfor %}

Docs and tipps on context processors:
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

benjamin


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to