← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 2 of 10

Views से Templates Render करना

एक view का काम एक worksheet (template) चुनना है और उसे answers का एक folder (context) देना है ताकि Django finished page print कर सके।
Syntax
markup
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 में।

Note: render() को manually HTML strings के साथ HttpResponse objects बनाने की तुलना में almost हमेशा prefer किया जाता है।

उदाहरण: The render() Shortcut

render() takes the request, the template path, and a context dict, returning a ready-to-send HttpResponse.

markup
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 कर सकता है।

Warning: अगर context में कोई key template के किसी {{ }} से match नहीं करती, तो Django उसे simply ignore कर देता है -- कोई error raise नहीं होता।

उदाहरण: Passing Data Through Context

The template can now use {{ username }} and {% if is_admin %} because those exact keys were passed in.

markup
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.

markup
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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Context dictionary pass करना भूल जाना, इसलिए template variables intended data के बजाय empty render होते हैं।
  2. Template path गलत तरीके से hardcode करना (app subfolder missing करना) असली templates/ directory layout से match करने के बजाय।
  3. 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 करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.