Template:
`<div id="messageBox_{{ forloop.counter0 }}" style="border:1px solid #ccc;background-color:white" onclick="document.getElementById ('messageBox_{{ forloop.counter0 }}').style.display = 'none';">
{% ifstartswith message "#ok#" %}
<font color="green">
{% endifstartswith %}
{% ifstartswith message "#error#" %}
<font color="red">
{% endifstartswith %}
{{ message|cut:"#ok#"|cut:"#error#" }}
</font>
</div>`
In a view you can now do something like that:
request.user.message_set.create(message="#ok#Hello User, this is a ok message!")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # -*- coding: utf-8 -*-
from django import template
from django.template import Node, NodeList, Variable, VariableDoesNotExist
register = template.Library()
def do_startswith(parser, token, negate):
try:
# split_contents() knows not to split quoted strings.
tag_name, string, start_string = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires two arguments" % token.contents.split()[0]
if not (start_string[0] == start_string[-1] and start_string[0] in ('"', "'")):
raise template.TemplateSyntaxError, "%r start strings argument should be in quotes" % tag_name
end_tag = 'end' + tag_name
nodelist_true = parser.parse(('else', end_tag))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse((end_tag,))
parser.delete_first_token()
else:
nodelist_false = NodeList()
return IfStartsWithNode(string, start_string, nodelist_true, nodelist_false, negate)
class IfStartsWithNode(Node):
def __init__(self, string, start_string, nodelist_true, nodelist_false, negate):
self.start_string, self.string = Variable(start_string), Variable(string)
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
self.negate = negate
self.negate = negate
def __repr__(self):
return "<IfStartsWithNode>"
def render(self, context):
try:
string = self.string.resolve(context)
except VariableDoesNotExist:
string = None
try:
start_string = self.start_string.resolve(context)
except VariableDoesNotExist:
start_string = None
if (self.negate and not string.startswith(start_string)) or (not self.negate and string.startswith(start_string)):
return self.nodelist_true.render(context)
return self.nodelist_false.render(context)
#@register.tag
def ifstartswith(parser, token):
return do_startswith(parser, token, False)
ifstartswith = register.tag(ifstartswith)
#@register.tag
def ifnotstartswith(parser, token):
return do_startswith(parser, token, True)
ifnotstartswith = register.tag(ifnotstartswith)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Better to do something like this instead:
#
Please login first before commenting.