Intermediate
What are Django views and how do they differ from templates?
In Django, views are Python functions or classes that handle the business logic of your application, while templates are HTML files used to present data to the user.
To create a view, follow these steps:
- Define a function in
views.py
. - Use
render()
to return a template. - Map the view to a URL in
urls.py
.
Example of a view:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')