Returning HTML templates lets your web app show nice pages to users. It connects your data with a design to make web pages.
Returning HTML templates in Django
from django.shortcuts import render def view_name(request): context = {'key': 'value'} # data to send to template return render(request, 'template_name.html', context)
render is a Django shortcut that combines loading a template and returning an HTTP response.
The context is a dictionary with data you want to show in the template.
from django.shortcuts import render def home(request): return render(request, 'home.html')
from django.shortcuts import render def profile(request): user_info = {'name': 'Alice', 'age': 30} return render(request, 'profile.html', user_info)
from django.shortcuts import render def items_list(request): items = ['apple', 'banana', 'cherry'] return render(request, 'items.html', {'items': items})
This view sends a name to the greeting.html template to show a personalized message.
from django.shortcuts import render def greeting(request): context = {'name': 'Friend'} return render(request, 'greeting.html', context)
Make sure your template files are in the correct folder (usually templates/ inside your app).
Context keys become variables you can use inside the HTML template with {{ key }} syntax.
Use Django's template language to add logic like loops and conditions inside your HTML.
Use render() to return HTML pages from views.
Pass data as a dictionary to show dynamic content in templates.
Templates and views work together to create user-friendly web pages.
Practice
Solution
Step 1: Understand Django view response methods
Django views can return HTML using several methods, but the most common and convenient isrender().Step 2: Identify the method that combines template rendering and response
render()takes the request, template name, and context data, then returns an HttpResponse with the rendered HTML.Final Answer:
render() -> Option AQuick Check:
Use render() to return HTML templates [OK]
- Confusing render() with redirect()
- Using HttpResponse() without rendering template
- Trying to use get_template() directly in views
home.html with an empty context in a Django view?Solution
Step 1: Recall the render() function signature
The correct order isrender(request, template_name, context).Step 2: Match the syntax with the correct order and parameters
return render(request, 'home.html', {}) matches the correct order and includes an empty dictionary for context.Final Answer:
return render(request, 'home.html', {}) -> Option AQuick Check:
render(request, template, context) syntax [OK]
- Swapping the order of arguments
- Omitting the context dictionary
- Passing context before request
{{ greeting }}?
def hello_view(request):
context = {'greeting': 'Hello, Django!'}
return render(request, 'hello.html', context)Solution
Step 1: Understand context dictionary usage
The context dictionary passes variables to the template. Here, 'greeting' is set to 'Hello, Django!'.Step 2: Template variable rendering
The template uses{{ greeting }}, which will be replaced by the value from context, 'Hello, Django!'.Final Answer:
Hello, Django! -> Option BQuick Check:
Context variables render their values in templates [OK]
- Expecting template to show variable name literally
- Forgetting to pass context dictionary
- Using wrong variable names in template
def index(request):
return render('index.html', request, {'title': 'Home'})Solution
Step 1: Check render() argument order
The correct order isrender(request, template_name, context). Here, the template name and request are swapped.Step 2: Identify the impact of wrong argument order
Swapping arguments causes a TypeError or unexpected behavior because render expects request first.Final Answer:
Arguments to render() are in wrong order -> Option DQuick Check:
render() needs request first, then template [OK]
- Swapping request and template arguments
- Forgetting to import render
- Passing context as a list instead of dict
Solution
Step 1: Understand passing data to templates
To show dynamic data like a list, pass it in the context dictionary to the template usingrender().Step 2: Use template loops to display list items
The template can loop overproductsto display each product. Other options do not properly render HTML with data.Final Answer:
Use render(request, 'products.html', {'products': products_list}) and loop in template -> Option CQuick Check:
Pass data in context dict for dynamic templates [OK]
- Trying to send data via redirect
- Returning raw data without template
- Embedding data in URL instead of context
Ready to prove it?
Pass the test and this pattern moves unknown
