Views से Templates Render करना
from django.shortcuts import render
def view_name(request):
context = {'key': value}
return render(request, 'app_name/template_name.html', context)
render() Shortcut
render() एक Django shortcut function है जो एक template load करता है, उसे एक context dictionary से भरता है, और result को एक HttpResponse में wrap करता है -- यह सब एक ही call में।
उदाहरण: The render() Shortcut
render() takes the request, the template path, and a context dict, returning a ready-to-send HttpResponse.
from django.shortcuts import render
def home(request):
context = {'site_name': 'CookiesCursor'}
return render(request, 'core/home.html', context)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Context के through Data Pass करना
Context dictionary की keys exactly वही variable names हैं जिन्हें आपका template {{ }} के साथ reference कर सकता है।
उदाहरण: Passing Data Through Context
The template can now use {{ username }} and {% if is_admin %} because those exact keys were passed in.
def profile(request):
context = {
'username': 'daniel',
'is_admin': False,
}
return render(request, 'core/profile.html', context)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Extra Context के बिना Rendering
अगर एक template को कोई dynamic data नहीं चाहिए, तो आप render() को सिर्फ request और template name के साथ call कर सकते हैं -- context argument optional है।
उदाहरण: Rendering Without Extra Context
Omitting context is valid when the template is fully static.
def about(request):
return render(request, 'core/about.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Context dictionary pass करना भूल जाना, इसलिए template variables intended data के बजाय empty render होते हैं।
- Template path गलत तरीके से hardcode करना (app subfolder missing करना) असली templates/ directory layout से match करने के बजाय।
- render() उपयोग करने के बजाय raw HTML strings के साथ एक HttpResponse return करना, template engine के benefits खोते हुए।
- render(request, template_name, context) data के साथ एक template render करने और एक HttpResponse return करने के लिए standard shortcut है।
- Context dictionary की keys template के अंदर available variable names बन जाती हैं।
- render() request-related context processors को आपके लिए अपने आप handle करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: