Views (FBV & CBV)
Snippets for Django views, covering function-based views (FBV) and class-based views (CBV). Learn to handle requests, responses, mixins, and common view patterns.
Basic Function-Based View with HttpResponse
Return plain text from a function-based view using HttpResponse.
Render Template in FBV with Context
Use render() to return an HTML template with context data.
Redirect Users in Views
Use redirect() or HttpResponseRedirect to navigate users.
Class-Based ListView Basics
Display a list of objects using DjangoÆs generic ListView.
Class-Based DetailView Basics
Show a single object using DjangoÆs DetailView.
CreateView with ModelForm
Handle object creation using CreateView and ModelForm.
UpdateView for Editing Objects
Edit existing objects using DjangoÆs UpdateView.
DeleteView with Confirmation
Delete objects safely with a confirmation page in DeleteView.
Override get_queryset in CBV
Customize which objects a CBV returns by overriding get_queryset().
Add Extra Context with get_context_data
Inject additional variables into templates via get_context_data().
Method Decorators on Class-Based Views
Apply decorators to class-based view methods using method_decorator.
Handle GET and POST in One FBV
Process GET to render a form and POST to handle submission in one view.
Return JSON with JsonResponse
Return JSON data from Django views using JsonResponse.
StreamingHttpResponse for Large Data
Stream large responses incrementally with StreamingHttpResponse.
FileResponse for Downloading Files
Serve files efficiently using FileResponse for downloads.
Custom 404 and 403 Error Views
Create custom handlers for 404 and 403 error pages.
Paginate ListView Results
Add paginate_by to ListView and render pagination controls.
Async Function-Based Views
Write async def views to handle I/O-bound tasks efficiently.
Async Methods in Class-Based Views
Implement async get()/post() methods in CBVs where appropriate.
Handle HTMX Requests in Views
Detect HTMX requests and return partial templates for snappy UX.