Posts

Showing posts from January, 2025

cros headers and csrf 403 forbidden errror solution

    install core headers and add it in installed apps SESSION_ENGINE = 'django.contrib.sessions.backends.file' SESSION_FILE_PATH = os.path.join(BASE_DIR,'sessions') SESSION_COOKIE_AGE = 86400       SESSION_EXPIRE_AT_BROWSER_CLOSE = False CORS_ORIGIN_ALLOW_ALL = True CORS_ALLOW_HEADERS = ['*'] CSRF_COOKIE_SAMESITE = 'Lax'  # Or 'Strict', depending on your setup CSRF_COOKIE_SECURE = False

Custom Middleware in django using session key

 from django.shortcuts import render,redirect class SessionAuthenticationMiddleware:     def __init__(self, get_response):         self.get_response = get_response     def __call__(self, request):         print(f"Request path: {request.path}, Session key: {request.session.get('session_key')}")         print("Middleware executed")  # Debugging line         session_key = request.session.get('session_key')         if not session_key:             print("Session key missing, redirecting...")  # Debugging line             # Allow access to the login page (which is at '/') and the signup page (which is at '/signup')          ...

python mail sent using thread

mail sent from django.core.mail import EmailMessage from django.template.loader import render_to_string from threading import Thread def send_email_threaded(otp, recipient_email):     """Helper function to send email asynchronously in a separate thread."""     try:         # Render the HTML template with the OTP         html_content = render_to_string(             'otp_email_template.html', {'otp': otp}         )         # Create the email object         email = EmailMessage(             subject="Your OTP for Login",  # Email subject             body=html_content,  # HTML content for the email    ...