from django import template
from django.contrib.messages.utils import get_level_tags
from django.utils.encoding import force_text


LEVEL_TAGS = get_level_tags()

register = template.Library()


@register.simple_tag()
def get_message_tags(message):
    """
    Returns the message's level_tag prefixed with Bootstrap's "alert-" prefix
    along with any tags included in message.extra_tags

    Messages in Django >= 1.7 have a message.level_tag attr
    """
    level_tag = force_text(LEVEL_TAGS.get(message.level, ''), strings_only=True)
    if level_tag == u"error":
        level_tag = u"danger"

    alert_level_tag = u"alert-{tag}".format(tag=level_tag)

    tags = [alert_level_tag]
    extra_tags = force_text(message.extra_tags, strings_only=True)
    if extra_tags:
        tags.append(extra_tags)

    return u" ".join(tags)

@@ -594,16 +594,38 @@
 anger%22%0A%0A
+    if level_tag:%0A    
     aler
@@ -679,21 +679,26 @@
 ag)%0A
-%0A    tags = %5B
+    else:%0A        
 aler
@@ -712,9 +712,16 @@
 _tag
-%5D
+ = None%0A
 %0A   
@@ -780,24 +780,25 @@
 _only=True)%0A
+%0A
     if extra
@@ -806,66 +806,201 @@
 tags
-:%0A        tags.append(extra_tags)%0A%0A
+ and alert_level_tag:%0A        return u' '.join(%5Bextra_tags, alert_level_tag%5D)%0A    elif extra_tags:%0A 
     
+   
 return 
-u%22 %22.join(tags)
+extra_tags%0A    elif alert_level_tag:%0A        return alert_level_tag%0A    return u''
 %0A

Handle instances where level_tag is undefined