Skip to content

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.

Let’s create a simple view in our myapp/views.py file that returns a basic HTML greeting:

myapp/views.py
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 HttpResponse from django.http.
  • We define a function hello that takes one argument: request, which is an HttpRequest object representing the incoming web request.
  • The function returns an HttpResponse object 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.py file (covered in the URL Mapping chapter).

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:

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:

myapp/views.py
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 render from django.shortcuts.
  • We create a context dictionary. Keys in this dictionary become variables available in the template.
  • render takes the request object, the path to the template file, and the context dictionary.
  • It renders the template with the context and returns an HttpResponse.

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:

myapp/views.py
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.

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