通过url链接设置,最后对应到satchmo/shop/views/home.py 中的home function。
def home(request, template="base_index.html"):
通过学习,本页涉及到相关知识如下:
相关知识¶
- 模板基础架构,div划分
- 模板变量很多,而且很多网页的有些设置是固定的,比如title等。django中如何处理这种情况。
- 如何理解satchmo中的config_value,以及config模块
- django 中的通用视图 之: Paginator, EmptyPage
页面布局¶
base_index.html集成自base.html。 base.html 采用3列框架。如下:
<div id="container">
<div id="top"> </div>
<div id="navbar"> </div>
<div id="leftnav"></div>
<div id="rightnav"></div>
<div id="content"></div>
<div id="footer"></div>
</div>
base_index.html重写了base.html中的navbar和content区域。
TEMPLATE_CONTEXT_PROCESSORS 解释¶
在模板中有许多通用变量(每个网页几乎都一样),比如{{ shop_name }},这些变量是如何被渲染赋值的呢? satchmo.shop.views.home.home中并没有制定shop_name的值。django用一种优雅的方式处理这个问题,就是 在setting.py 中设置 TEMPLATE_CONTEXT_PROCESSORS
-
::
-
-
TEMPLATE_CONTEXT_PROCESSORS = (‘satchmo.shop.context_processors.settings’,
- ‘django.core.context_processors.auth’, ‘satchmo.recentlist.context_processors.recent_products’, ‘django.core.context_processors.i18n’, )
跟踪django源码,摘录相关内容如下:
class RequestContext(Context):
"""
This subclass of template.Context automatically populates itself using
the processors defined in TEMPLATE_CONTEXT_PROCESSORS.
Additional processors can be specified as a list of callables
using the "processors" keyword argument.
"""
def __init__(self, request, dict=None, processors=None):
Context.__init__(self, dict)
if processors is None:
processors = ()
else:
processors = tuple(processors)
for processor in get_standard_processors()
self.update(processor(request))
# want it to execute if somebody uses RequestContext.
def get_standard_processors():
global _standard_context_processors
if _standard_context_processors is None:
processors = []
for path in settings.TEMPLATE_CONTEXT_PROCESSORS:
综合上面代码,可以看出,django在渲染模板的时候,会把settings.TEMPLATE_CONTEXT_PROCESSORS内processor 的字典也加上。结合实例看看 satchmo.shop.context_processors.settings 就能明白。
config模块¶
从
count = config_value('SHOP','NUM_PAGINATED')
说起, 其中的config模块的ConfigurationSettings类就是遵循singleton设计。 每个模块的通过 config.py 中的 config_register()函数配置相关值。 比如shop.models 就 含有import config。语句,这样就可以配置相关信息。
这个模块有可重用价值,在邮件列表中已有人讨论
On Wed, Oct 1, 2008 at 5:34 AM, Erik Allik <racecon@gmail.com> wrote:
Hi everybody,
Is satchmo.configuration currently in a state of being able to reuse
it in other, non-satchmo projects?
Erik
Yes, with the exception that it requires satchmo.caching and satchmo.utils.
Also, the automatic base configuration group is called "SHOP_GROUP."
You can ignore the SHOP_GROUP for outside-of-Satchmo use.
通用视图¶
通用视图就是django中对一些常用视图(view)的封装,我们在使用的时候只需要简单 的传递渲染参数。就可以得到想要的结果。
django book 的第九章中对此有专门表述。中文连接翻译如下: 通用视图
satchmo 首页中用到了Paginator。用法可以查看django的 官方文档Paginator
也可参照官网文档的 简化翻译