Explode, Implode, Rtrim in Python Django

Explode, Implode, Rtrim in Python Django like php
# Explode in python:
temp = temp1.split(",")
# Implode in python:
temp2 = ','.join([str(i) for i in temp])
# rtrim() in python:
temp = name.rstrip(',')
# Implode Array in python:
temp_Arr = ['FurnitureMakeover', 'nurserydresser', 'funcycled', 'dresser']

temp_Tags = ','.join(temp_Arr)

To strip/remove HTML tags from an existing string we can use the strip_tags function.

To strip/remove HTML tags from an existing string we can use the strip_tags function:
# import the strip_tags
from django.utils.html import strip_tags
# simple string with html inside.
html = '<p>paragraph</p>'

print html # will produce: <p>paragraph</p>

stripped = strip_tags(html)

print stripped # will produce: paragraph
# This is also available as a template tag:
{{ somevalue|striptags }}
# If you want to remove only specific tags you need to use the removetags
from django.template.defaultfilters import removetags
html = '<strong>Bold...</strong><p>paragraph....</p>'

stripped = removetags(html, 'strong') # removes the strong only.

stripped2 = removetags(html, 'strong p') # removes the strong AND p tags.
# Also available in template:
{{ value|removetags:"a span"|safe }}

How to Add a Custom Function in Jquery Form Validator

How to Add a Custom Function in Jquery Form Validator?

 

First, write the custom function like below.

jQuery.validator.addMethod("mobile_prefix", function(value, element) 
{

        var select=true;

        var mobile_prefix=$("#mobile_prefix").val();

        if(value && !mobile_prefix)
        {
            select=false;
        }

        return select;

    });

 

Secondly, In the form validator function call the above custom defined function. It will return TRUE / FALSE.

 

 $("#form").validate({

        rules:{
            "password":{
                required:true,
                noSpace: true
            },
            "confpassword": {
                equalTo:"#passwordd"
            },
            "mobile_number":{
                required:true,
                requiredcustom:true,
                numericvalue:true,
                mobile_prefix:true
            },
          },  

          messages: {
            "confpassword": {
                required:"Enter confirm password",
                equalTo: "Password mismatch",
            },
            "password":{
                required:"Enter password",
                noSpace : "no space please."
            },
            "mobile_number": {
                required: "Enter Mobile Number",
                minlength: "Mobile number atleast 10",
               mobile_prefix:"Please select code"
            },
        },
    });

If you like the post, please comment below & follow us on social platform.

How to Stop Date Picker Date change while using Google Translator

Stop Date Picker Date change while using Google Translator

 

In the Website, when  changing the language from English to other Language like swedish, chinese or any other language, the Date picker value is changing & showing like NaN/NaN/NaN.

But, the Datepicker format is mm/dd/yyyy

Solution:

For, these we have to find the Date picker container where date i showing. Then find the below classes added in that container,

ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all

Then Add a new class with them, “notranslate”

ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all notranslate

If Google find this class in the container, Google will not translate the content of the container.

keyword “notranslate” will prevent your calendar to translate.

We, can add class using this function, like

$(function() {
    $(".datepicker").datepicker();
    $('.ui-datepicker').addClass('notranslate');
});

 

If you like our post, Please Comment & share.

When To Use __repr__ vs __str__? in Python Django

 

# When To Use __repr__ vs __str__?
# Emulate what the std lib does:

>>> import datetime
>>> today = datetime.date.today()

# Result of __str__ should be readable:

>>> str(today)
'2017-02-02'

# Result of __repr__ should be unambiguous:

>>> repr(today)
'datetime.date(2017, 2, 2)'

# Python interpreter sessions use
# __repr__ to inspect objects:


>>> today
datetime.date(2017, 2, 2)