Pinax源代码分析7 - Profiles
负责管理用户的档案
url
(r'^profiles/', include('profiles.urls')),
逻辑与模板
url(r'^username_autocomplete/$', 'misc.views.username_autocomplete_friends', name='profile_username_autocomplete'),
用misc app 的 username_autocomplete_all 处理,依赖于basic_profiles和avatar。
from basic_profiles.models import Profile
from avatar.templatetags.avatar_tags import avatar
url(r'^$', 'profiles.views.profiles', name='profile_list'),
profiles view 会将所有用户的query(User.objects.all())传递给模板 profiles/profiles.html
{% extends "profiles/base.html" %}
{% load pagination_tags %}
{% load avatar_tags %}
{% load humanize %}
{% block body %}
{% autopaginate users %}
{% regroup users by date_joined.date as users_by_date_joined %}
{% for date in users_by_date_joined %}
<h2 class="join_date">{{ date.grouper|naturalday:_("MONTH_DAY_FORMAT")|capfirst }}</h2>
{% for user in date.list %}
<div style="float: left;">{% avatar user 40 %}</div>
{% endfor %}
{% endfor %}
{% paginate %}
{% endblock %}
模板扩展了profiles的base.html模板,base 负责提供子导航的内容。
pagination_tags 是 django_pagination 的 pagination app 的自定义标签。profiles.html使用{% autopaginate users %}对users进行分页, 使用{% paginate %}显示页码和页码范围等信息。
avatar_tags 是 django_avatar 的 avator app 的自定义标签。使用{% avatar user 40 %}来获得显示用户头像的标签。
humanize 是 Django 自带的,在django.contrib 目录下。这里使用naturalday标签,如果日期是今天就会显示today,相似的有昨天和明天的判断。
暂时没有找到regroup标签是在哪里定义的。
url(r'^(?P<username>[/w/._-]+)/$', 'profiles.views.profile', name='profile_detail'),
profile view 会判断浏览者和被浏览者是不是一个人、好友、follow关系。并且依此来处理和显示档案相关的表单。
{% extends "profiles/base.html" %}
{% load app_plugins %}
{% plugin_point "profiles.profile" user=other_user owner=other_user viewer=user %}
app_plugins 是 app_plugins app 定义的自定义标签,没太看明白 plugin_point 的用法。修改代码为:
<br />---------------------------------------------------------
{% plugin_point "profiles.profile" user=other_user owner=other_user viewer=user %}
<br />---------------------------------------------------------
结果页面为:
看来这一行生成了不少东西。查阅app_plugins的官方http://code.google.com/p/django-app-plugins/可知,这个app主要实现的是一个反向的{% block %}, 每个app在自己的模板目录里的foo.html文件中定义将要被包含的内容,其它模板调用{% plugin_point "foo" %}的时候就会被包含进去。例如pinax中,wiki的模板中有plugins/profiles/profile.html,而在profile.html模板中有如下一句:
{% plugin_point "profiles.profile" user=other_user owner=other_user viewer=user %}
这就将wiki在plugins/profiles/profile.html中的内容包含进来了。