Source code for django_landing_simple.landing_simple.views
"""This module implements a simple, public-facing landing page for web applications. It providesa minimal entry point for visitors with multi-language support through language selectionfunctionality and Google Analytics integration for visitor tracking.**Business Context:**The landing_simple app serves as a lightweight, standalone landing page solution for webapplications that don't require user authentication or complex workflows. It's designed forinformational websites, marketing pages, or simple web applications where the primary goalis to present information to visitors and allow them to select their preferred language.**Key Business Features:**1. **Landing Page:**- :func:`home`: Simple, static home page for all visitors - No authentication or user management required - Responsive design for all devices - Multi-language support for international audiences - Google Analytics integration for visitor tracking2. **Language Selection:**- :func:`set_language`: Users can select their preferred language - Language choice persists via cookie - Affects entire application interface - Supports all languages defined in Django settings - Immediate language activation for current request**User Journey:**1. **First-Time Visitor:**- Arrives at home page - Views content in default language - Can select preferred language from language selector - Language preference saved for future visits - Navigates through application in selected language2. **Returning Visitor:**- Arrives at home page - Content displayed in previously selected language (via cookie) - Can change language at any time**Technical Features:**- Google Analytics (GA4) integration via Measurement ID- Language cookie management with Django settings- Multi-language support via Django i18n framework- Simple, lightweight implementation- No database dependencies- No session management required**Integration:**The landing_simple app integrates with:- Django's internationalization framework for multi-language support- Google Analytics for visitor tracking and metrics- Django settings for configuration (GOOGLE_MEASUREMENT_ID, LANGUAGES)**Security:**- No authentication required (public access)- Safe handling of invalid language codes- No sensitive data exposure- No user data collection (beyond Google Analytics)**Use Cases:**- Simple marketing landing pages- Product information websites- Service description pages- Static content delivery- Multi-language informational sites- Websites without user authentication requirements"""# Initialize Django settings if running standalone (e.g., in Thonny IDE)# This allows the module to be imported for inspection without running Django serverimportsysimportosif__name__=="__main__"or'django.conf.settings'notinsys.modulesornothasattr(sys.modules.get('django.conf.settings',None),'configured'):importdjangofromdjango.confimportsettingsifnotsettings.configured:# Add the project root to Python pathproject_root=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))ifproject_rootnotinsys.path:sys.path.insert(0,project_root)# Set the Django settings moduleos.environ.setdefault('DJANGO_SETTINGS_MODULE','settings.settings')# Setup Djangodjango.setup()fromdjango.shortcutsimportredirect,renderfromdjango.urlsimportreversefromdjango.confimportsettingsfromdjango.utilsimporttranslationfromdjango.httpimportHttpResponseRedirect# get the Google Measurement ID from Django’s "settings.py"google_measurement_id=getattr(settings,"GOOGLE_MEASUREMENT_ID",None)# get the installed languages from Django’s "settings.py"installed_languages=getattr(settings,"LANGUAGES",None)
[docs]defhome(request):""" Prepare the home page. """ifrequest.method=="GET":context={'google_measurement_id':google_measurement_id,'installed_languages':installed_languages,}returnrender(request,"home.html",context)elifrequest.method=="POST":context={'google_measurement_id':google_measurement_id,'installed_languages':installed_languages,}returnrender(request,"home.html",context)
[docs]defset_language(request):""" Handle language selection by the user and set the appropriate language cookie. """# Get the URL to redirect to after language is set (default to '/')next_url=request.POST.get('next','/')# Get the language code from POST datalang_code=request.POST.get('language')# If the language code is valid (exists in settings.LANGUAGES)iflang_codeandlang_codeindict(settings.LANGUAGES):# Prepare the redirect responseresponse=HttpResponseRedirect(next_url)# Set the language cookie manuallyresponse.set_cookie(settings.LANGUAGE_COOKIE_NAME,# usually 'django_language'lang_code,max_age=settings.LANGUAGE_COOKIE_AGE,# how long the cookie lasts (seconds)path=settings.LANGUAGE_COOKIE_PATH,# usually "/"secure=settings.LANGUAGE_COOKIE_SECUREorNone,# only over HTTPS if configured)# Activate the selected language for this request immediatelytranslation.activate(lang_code)request.LANGUAGE_CODE=translation.get_language()returnresponse# If invalid language code, still redirect without setting anythingreturnHttpResponseRedirect(next_url)