On 15 Jun., 06:55, Joel Klabo <joelkl...@gmail.com> wrote:
> I am working on a simple site right now and the views are pretty easy.
> Usually just iterate a loop of objects. But, I am trying to figure out
> how a website with all different kinds of data, including a sign in
> form, is setup in django. Is that all in one big view? Or, is there
> some way to combine them?
>
> Any insight would be helpful, thank you.

Hi Joel,

very good question, I take it you want to know how "composition" is
done at the "page level" without dragging all the logic into one view.
In django you can generate recurring elements with template tags, for
adding variables to the request or generate markup directly.

say you want a list of users that joined your site recently:

class RecentUsersNode(template.Node):
    def __init__(self, limit):
        try: self.limit = int(limit)
        except ValueError: self.limit = 10

    def render(self, context):
        profiles = UserProfile.objects.order_by('user__date_joined')
[:self.limit]
        context['recent_users'] = profiles
        return ''

@register.tag
def recent_users(parser, token):
    args = token.split_contents()
    if len(args) <= 1:
        return RecentUsersNode(10)

    num = args[1]
    if(num[0] == num[-1] and num[0] in ('"', "'")):
        num = num[1:-1] # strip quotes
    return RecentUsersNode(num)



{% recent_users %} will add a list of UserProfile objects to your
request and you can use it like:

{% for profile in recent_users %}
  <img src="{{ profile.photo.url }}" alt="{{ profile.desc }}" />
{% endfor %}


In the old days of web 1.0 there was a natural 1:1 mapping from the
page to a script/function which generates the content. With ajax it's
becoming more like a desktop app where you have many parts/widgets
with callbacks or events. If you want the page to display without JS,
you still have to generate the page with one request though...

cheers
 Paul

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

Reply via email to