Django - Creating Views
Django - Creating Views
Section titled “Django - Creating Views”In Django, a view is a Python function or class that takes a web request and returns a web response. This response can be the HTML content of a web page, a redirect, a 404 error, an XML document, a JSON response, an image, or anything else the browser can display. Views contain the business logic of your application.
Views are typically defined in the views.py file within your Django app.
Simple Function-Based View (FBV)
Section titled “Simple Function-Based View (FBV)”Let’s create a simple view in our myapp/views.py file that returns a basic HTML greeting:
from django.http import HttpResponse
def hello(request): html_content = "<h1>Welcome to my app!</h1>" return HttpResponse(html_content)In this view:
- We import
HttpResponsefromdjango.http. - We define a function
hellothat takes one argument:request, which is anHttpRequestobject representing the incoming web request. - The function returns an
HttpResponseobject containing the HTML we want to display. - To make this view accessible via a URL, you need to map a URL pattern to it in your
urls.pyfile (covered in the URL Mapping chapter).
Using Templates
Section titled “Using Templates”Hardcoding HTML directly in views is generally bad practice. Django’s template system allows you to separate presentation (HTML) from logic (Python). Let’s modify the view to use a template.
First, create a template file. By convention, templates for an app myapp are often placed in myapp/templates/myapp/. Create myapp/templates/myapp/hello.html:
<!DOCTYPE html><html><head> <title>Hello Page</title></head><body> <h1>{{ greeting }}</h1></body></html>Note: Ensure your settings.py has APP_DIRS=True in the TEMPLATES setting so Django can find app templates automatically.
Now, update the view in myapp/views.py to use the render shortcut:
from django.shortcuts import render
def hello(request): context = {'greeting': 'Welcome to my app, rendered via template!'} return render(request, 'myapp/hello.html', context)Here:
- We import
renderfromdjango.shortcuts. - We create a
contextdictionary. Keys in this dictionary become variables available in the template. rendertakes therequestobject, the path to the template file, and thecontextdictionary.- It renders the template with the context and returns an
HttpResponse.
Views with Parameters
Section titled “Views with Parameters”Views often need to process parameters passed in the URL. For example, displaying a specific item based on its ID.
Let’s create a view that accepts a number from the URL:
from django.http import HttpResponse
def show_number(request, number): html_content = f"<h1>Displaying number: {number}!</h1>" return HttpResponse(html_content)This view now takes an additional argument, number. How this parameter is captured from the URL is defined in urls.py. We use an f-string (Python 3.6+) for cleaner string formatting.
Class-Based Views (CBVs)
Section titled “Class-Based Views (CBVs)”Django also supports class-based views, which can help organize code, promote reuse through inheritance, and handle common patterns (like displaying forms or lists of objects) more effectively. Generic CBVs provided by Django are particularly useful (covered later).
Further Reading: Django Views Documentation