Django实现自定义标签
时间:2009-08-24 来源:hkebao
1.要实现一个功能数据集里面的作循环然后将其中的一个值传到下一个当数据集的条件想了一下只能是考虑使用 自定义标签的办法了!
from django import template
from django.template import Context, Template, loader, resolve_variable
register = template.Library()
class PostDetailNode(template.Node):
def __init__(self, id, current_post_id):
self.id = id
self.current_post_id = current_post_id
pass
def render(self, context):
current_post_id = int(resolve_variable(self.current_post_id, context))
context['current_post_id'] = current_post_id
t = loader.get_template("forum/templatetags/post_detail.html")
return t.render(context)
#在这里面我们可以再写一个模板页面进来的!这样的话就可以实现大循环里面的每一个子循环都将会自动加载进来 这个模板的!
#@register.tag(name='post_detail_tag')
def do_post_detail_tag(parser, token):
try:
#tag_name, args = token.contents.split(None, 1)
#id, current_post_id = args.split(None, 1)
tag_name, id, current_post_id = token.split_contents()
print id, current_post_id
except ValueError:
raise template.TemplateSyntaxError, "%s tag requires argument" % tag_name
return PostDetailNode(id, current_post_id)
register.tag('post_detail_tag', do_post_detail_tag)
from django import template
from django.template import Context, Template, loader, resolve_variable
register = template.Library()
class PostDetailNode(template.Node):
def __init__(self, id, current_post_id):
self.id = id
self.current_post_id = current_post_id
pass
def render(self, context):
current_post_id = int(resolve_variable(self.current_post_id, context))
context['current_post_id'] = current_post_id
t = loader.get_template("forum/templatetags/post_detail.html")
return t.render(context)
#在这里面我们可以再写一个模板页面进来的!这样的话就可以实现大循环里面的每一个子循环都将会自动加载进来 这个模板的!
#@register.tag(name='post_detail_tag')
def do_post_detail_tag(parser, token):
try:
#tag_name, args = token.contents.split(None, 1)
#id, current_post_id = args.split(None, 1)
tag_name, id, current_post_id = token.split_contents()
print id, current_post_id
except ValueError:
raise template.TemplateSyntaxError, "%s tag requires argument" % tag_name
return PostDetailNode(id, current_post_id)
register.tag('post_detail_tag', do_post_detail_tag)
需要写一个django的自定义标签,需求很简单,根据标签里的token得到相应的model的queryset,放context里就行了。
Python代码- {% load_model oa.Department %}
{% load_model oa.Department %}
我可以用{{ department }}得到oa.models 里的 Department所有的值,或者
Python代码- {% load_model oa.Department depart %}
{% load_model oa.Department depart %}
用{{ depart }} 得到。
那么首先,我在某个已经安装的app里面的templates文件夹里新建一个文件loadModelTag.py。注意在templates里需要有__ini__.py。那么可以开始写程序了
Python代码
- #coding=utf-8
- import sys
- from django import template
- from oa.models import *
- register = template.Library()
- @register.tag(name="load_model")
- def do_load_model(parser,token):
- tokenNum = len(token.contents.split())
- name = None
- try:
- if tokenNum == 2:
- tag_name, path = token.split_contents()
- else:
- tag_name, path , name= token.split_contents()
- except ValueError:
- raise template.TemplateSyntaxError, \
- "%r tag TemplateSyntaxError" % token.contents.split()[0]
- mod_name,model_name=path.split('.')
- mod = sys.modules[mod_name]
- if model_name not in mod.models.__dict__:
- raise template.TemplateSyntaxError, "no model named %s"%model_name
- else:
- model = mod.models.__dict__[model_name]
- if name:
- model_name=name
- return load_moldelNode(model,model_name)
- class load_moldelNode(template.Node):
- def __init__(self, model, model_name):
- self.model_name = model_name
- self.model = model
- def render(self, context):
- context[self.model_name.lower()]=self.model.objects.all()
#coding=utf-8 import sys from django import template from oa.models import * register = template.Library() @register.tag(name="load_model") def do_load_model(parser,token): tokenNum = len(token.contents.split()) name = None try: if tokenNum == 2: tag_name, path = token.split_contents() else: tag_name, path , name= token.split_contents() except ValueError: raise template.TemplateSyntaxError, \ "%r tag TemplateSyntaxError" % token.contents.split()[0] mod_name,model_name=path.split('.') mod = sys.modules[mod_name] if model_name not in mod.models.__dict__: raise template.TemplateSyntaxError, "no model named %s"%model_name else: model = mod.models.__dict__[model_name] if name: model_name=name return load_moldelNode(model,model_name) class load_moldelNode(template.Node): def __init__(self, model, model_name): self.model_name = model_name self.model = model def render(self, context): context[self.model_name.lower()]=self.model.objects.all()
我们可以在模板中使用这个tag了,
首先要载入标签文件
{% load loadModelTag %}
然后就可以使用
{% load_model oa.Department %}
context中已经有department了
转自:http://vansention.javaeye.com/blog/362867
相关阅读 更多 +