Django Templates & Dynamic Data

📘 Day 84: Django – Templates & Dynamic Data 🔹 What are Templates? • Templates are used to create the frontend (HTML pages) in Django • They display data sent from the backend • Helps separate design from logic 🔸 Templates Folder Setup • A templates folder is created inside the app • This folder stores all HTML files 🔹 Important Step: • Must register the templates folder in settings.py (DIRS section) • Without this → Django cannot find HTML files 🔸 HTML Page Creation • Create HTML files inside the templates folder • These act as the UI of the application 🔹 Key Idea: • HTML is static • Django makes it dynamic 🔸 Rendering Templates • To display an HTML page → use render() in views 🔹 Flow: • URL → View → Template 💡 render(): • Connects backend (views) with frontend (HTML) • Sends data to HTML 🔸 Django Variables {{ }} • Used to display dynamic data in HTML 🔹 Key Points: • Written inside {{ }} • Values come from views (dictionary data) • Keys in dictionary = variables in template 💡 Example Concept: • Backend sends → name = "Adam" • Frontend displays → {{ name }} 🔸 Passing Data to Templates • Data is passed as a dictionary from views 🔹 Key Points: • Keys → variable names • Values → actual data • Can pass directly or using a variable 🔸 Django Template Tags {% %} • Used to add logic inside HTML 🔹 Used For: • Conditions (if, else) • Loops (for) 🔸 Conditional Rendering • Templates can check conditions like Python 🔹 Key Points: • {% if %}, {% elif %}, {% else %} • Must end with {% endif %} 💡 Example Concept: • If age ≥ 18 → show eligible • Else → show not eligible 🔸 Execution Flow User opens URL URL calls view View sends data Template displays data ✨ Today you learned: • What templates are and how they work • How to connect HTML with Django using render() • How to use dynamic variables {{ }} • How to use logic inside HTML with {% %} This is where Django becomes powerful—combining backend + frontend seamlessly 🚀 #Django #Python #WebDevelopment #Frontend #BackendDevelopment #Day84 #Templates #FullStack #CodingJourney

To view or add a comment, sign in

Explore content categories