Dimitris Kougioumtzis Blog About technology

Django custom simple tags

Category Django

Posted on Aug. 17, 2018



Django custom simple tags


 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
from django import template
from django.urls import reverse_lazy
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag
def create_title(model, value):
    """
    example:  category add
    """
    if value in ['detail', 'update', 'delete']:
        if hasattr(model, 'pk'):
            model_name = model
    else:
        model_name = model.__class__.__name__
    return '{} {}'.format(model_name, value)


@register.simple_tag
def create_url(model, value):
    """
    example:  category-create
    """
    model_name = model.__class__.__name__.lower()
    url = "manage-{}-{}".format(model_name, value)
    return reverse_lazy(url)


@register.simple_tag
def create_caption(model, value):
    """
    example:  Create Category
    """
    caption = value
    model_name = model.__class__.__name__.lower()
    title = '{} {}'.format(caption, model_name)
    return mark_safe(title.title())


@register.simple_tag
def object_url(model, value):
    """
    example:  category-update
    """
    model_name = model.__class__.__name__.lower()
    url = "manage-{}-{}".format(model_name, value)
    return reverse_lazy(url, kwargs={'pk': model.pk})


@register.simple_tag
def list_url(model, value):
    """
    example:  category-list
    """
    model_name = model.__class__.__name__.lower()
    url = "manage-{}-{}".format(model_name, value)
    return reverse_lazy(url)

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere