Redis Administration
Feb 19, 2021 | 1163 views
Secure Redis:
Django Redis UnpicklingError:
Using Redis in Python:
Comments: 0Tags: Django
Feb 19, 2021 | 1163 views
Secure Redis:
Django Redis UnpicklingError:
Using Redis in Python:
Comments: 0Oct 09, 2020 | 806 views
here is my views.py bases on above link (also its code on github):
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.core.cache import cache
from django.conf import settings
from django.core.cache.backends.base import DEFAULT_TIMEOUT
import string, random
CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)
from .models import Product
# Create your views here.
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def price_generator(size=3, chars=string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def create_product(name, desc='', price=0):
product = Product(name=name, description=desc, price=price)
product.save()
@api_view(['GET'])
def view_books(request):
products = Product.objects.all()
if len(products) < 1000:
i = 0
while i < 1000 - len(products):
create_product(id_generator(), desc=3*id_generator(), price=int(price_generator()))
i += 1
results = [product.to_json() for product in products]
return Response(results, status=status.HTTP_201_CREATED)
@api_view(['GET'])
def view_cached_books(request):
if 'product' in cache:
# get results from cache
products = cache.get('product')
return Response(products, status=status.HTTP_201_CREATED)
else:
products = Product.objects.all()
results = [product.to_json() for product in products]
# store data in cache
cache.set('product', results, timeout=CACHE_TTL)
return Response(results, status=status.HTTP_201_CREATED)
Here is requirements.txt (Be aware djangorestframework version):
$ pip freeze
asgiref==3.2.10
Django==1.9
django-redis==4.12.1
djangorestframework==3.6.3
pytz==2020.1
redis==3.5.3
sqlparse==0.4.1
Aug 10, 2020 | 1167 views
Solution refer to below links
Python re.sub() case insensitive replace coding example:
In [54]: re.sub('(%s)'%re.escape('hE'), r'<abc>\g<1></abc>', 'hello', flags=re.IGNORECASE)
Out[54]: '<abc>he</abc>llo'
In [56]: re.sub('(%s)'%re.escape('hE.'), r'<abc>\g<1></abc>', 'he.llo', flags=re.IGNORECASE)
Out[56]: '<abc>he.</abc>llo'
Or another version (Here we exclude URL replacement from HTML text):
pattern = re.compile('(^%s|[\s]%s|%s[\s])' % tuple([re.escape(word),]*3), re.IGNORECASE)
highlighted = pattern.sub(r'<span class="highlight">\g<1></span>', highlighted)
Regex resource refer to:
Comments: 0Aug 05, 2020 | 736 views
Refer to
Comments: 0Aug 04, 2020 | 996 views
Refer to
When creating a new database, remember to create with the right collate settings:
CREATE DATABASE foo CHARACTER SET utf8 COLLATE utf8_general_ci;
Error Number: 1267
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
Solutions refer to :
config on /etc/my.cnf[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8