Dimitris Kougioumtzis Blog About technology

django custom template filters

Category Django

Posted on Oct. 11, 2017



django custom template filters


  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from django import template
from django.urls import reverse_lazy
register = template.Library()


@register.inclusion_tag('ajax/button_partial.html')
def show_modal_button(model):
    '''
    model is object

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy('{}-{}-create'.format(app, model_name))
    return {'model_name': model_name, 'url': url_link}


@register.filter(name='url_list')
def url_list(model):
    '''
    model is object

    url convention:

    {{app_name}} - {{model}} - list

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy('{}-{}-list'.format(app, model_name))
    return url_link


@register.filter(name='url_create')
def url_create(model):
    '''
    model is object

    url convention:

    {{app_name}} - {{model}} - create

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy('{}-{}-create'.format(app, model_name))
    return url_link


@register.filter(name='url_detail')
def url_detail(model, pk):
    '''
    model is object

    url convention:

    {{app_name}} - {{model}} - detail

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy(
        '{}-{}-detail'.format(app, model_name), kwargs={'pk': pk})
    return url_link


@register.filter(name='url_update')
def url_update(model, pk):
    '''
    model is object
    url convention:

    {{app_name}} - {{model}} - update
    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy(
        '{}-{}-update'.format(app, model_name), kwargs={'pk': pk})
    return url_link


@register.filter(name='url_delete')
def url_delete(model, pk):
    '''
    model is object

    url convention:

    {{app_name}} - {{model}} - delete

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    url_link = reverse_lazy(
        '{}-{}-delete'.format(app, model_name), kwargs={'pk': pk})
    return url_link


@register.filter(name='model_name')
def model_name(value):
    '''
    value is model object
    '''
    model = value.__class__.__name__.capitalize()
    return model


@register.filter(name='model_name_lower')
def model_name_lower(model):
    '''
    model is object

    '''
    app = model._meta.app_label
    model_name = model.__class__.__name__.lower()
    return model_name

About

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

Elsewhere