Dive into Code

Discover code snippets, tutorials, and programming insights

Django

django template tag for boolean image

<p>This code is a custom Django template tag named <code>get_boolean_img</code>. It&#39;s used to render a specific HTML icon based on a boolean value (True or False).</p> <p>&nbsp;</p> <ol> <li> <p>The …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django import template
from django.utils.html import format_html
from django.utils.safestring import mark_safe
register = template.Library()


@register.simple_tag
def get_boolean_img(value):
    if value:
        return format_html(mark_safe('<i class="bi bi-check-lg"></i>'))
    return format_html(mark_safe('<i class="bi bi-x"></i>'))
jQuery

Jquery function for django display error data

<p>This code is a JavaScript function named <code>display_error_data(data)</code>. It seems to handle error data received as input and displays the errors in the console and on the web page as …

javascript
1
2
3
4
5
6
7
8
function display_error_data(data){
      console.info("error:",data);
      $.each(data['errors'], function( index, value ) {
        console.info(index, ':', value);
        $("[name='" +index + "']").addClass('is-invalid');
        $("[name='" +index + "']").after("<div class='invalid-feedback'>"+ value + "</div>");
      });
    }
jQuery

Handlebars checkbox helper

Handlebars checkbox helper

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Handlebars.registerHelper("makeCheckBox", function(name, label, value){
        let output = '<div class="form-check">';
        output += '<input class="form-check-input" name="'+ name + '"' +'type="checkbox" value="' + value + '"'
        console.info(typeof(value), value);
        if(value === true){
             output += 'checked ';
        }
        output +='id="' + name + 'Default"' + '>';
        output +='<label class="form-check-label" for="' + name + 'Default">' +
                    label + '</label></div>';
        return output;
    });
jQuery

Handlebars multipleselectbox helper

Handlebars multipleselectbox helper

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Handlebars.registerHelper("makeSelectBoxMultiple", function(name, value, options){
       let items = options;
       let output = '<label for="'+name+'" class="form-label">'+name+'</label><select class="form-control" name="'+name +'" multiple="multiple">'
       for(let val in items){
         let id = parseInt(items[val].id);
         let selected = false;
         for(let item in value){
             if(parseInt(value[item].id)==id){
               selected = true;
             }
         }
         if(selected==true){
           output +='<option value="'+ id +'" selected>'+items[val].name +'</option>';
         }
         else{
             output +='<option value="'+ id +'">'+items[val].name +'</option>';
         }
       }
       output += '</select>'
       return output;
     });