python

python
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class Comment(models.Model):

Django contenttypes framework for generic relations

django python models
by Priya Sharma 3 tabs
python
from django.core.cache import cache
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator
from django.views.generic import ListView, DetailView
from .models import Post

Django caching with cache_page and cache decorator

django python caching
by Priya Sharma 1 tab
python
INSTALLED_APPS += ['silk']

MIDDLEWARE += ['silk.middleware.SilkyMiddleware']

# Silk configuration
SILKY_PYTHON_PROFILER = True

Django performance monitoring with django-silk

django python performance
by Priya Sharma 3 tabs
python
from argon2 import PasswordHasher

password_hasher = PasswordHasher(
    time_cost=3,
    memory_cost=65536,
    parallelism=4,

Password hashing with Argon2 and bcrypt migration paths

passwords argon2 bcrypt
by Kai Nakamura 1 tab
python
import functools
import threading


def debounce(wait):
    def decorator(func):

Debounce Repeated Function Calls With a Thread-Safe Python Decorator

python decorators debounce
by codesnips 2 tabs
python
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.template.loader import render_to_string
from django.conf import settings
import os

Django email with attachments and templates

django python email
by Priya Sharma 1 tab
python
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',

Django redis caching strategies

django python redis
by Priya Sharma 2 tabs
python
import json
from channels.generic.websocket import AsyncWebsocketConsumer


class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):

Django channels for WebSockets and async

django python websockets
by Priya Sharma 2 tabs
python
from django.core.exceptions import ValidationError


def validate_file_size(file):
    """Limit file size to 5MB."""
    max_size_mb = 5

Django file upload handling with validation

django python files
by Priya Sharma 2 tabs
python
from rest_framework import routers
from rest_framework_nested import routers as nested_routers
from . import views

router = routers.DefaultRouter()
router.register(r'posts', views.PostViewSet, basename='post')

Django REST Framework nested routers

django python rest
by Priya Sharma 2 tabs
python
import os
from .base import *

DEBUG = False

SECRET_KEY = os.environ['SECRET_KEY']

Django deployment checklist and production settings

django python deployment
by Priya Sharma 1 tab
python
import random


class PrimaryReplicaRouter:
    """Route reads to replicas, writes to primary."""

Django database routers for multiple databases

django python database
by Priya Sharma 1 tab