GE04 ‐ 1 ‐ Views (David Jones) - wycre/CS3300-Team-2-9 GitHub Wiki
The views contain all the business logic of your website and are written in views.py
A view is simply a function that returns an HTTP response based on an HTTP request. These functions are called by the Django manager after they are mapped in urls.py. A URL will map to a view, and the request made to that URL will be passed as a parameter to the view.
A view function can look like:
def index(request):
return render(request, 'portfolio_app/index.html')
In our views return the HTTP response that is returned by the render function from django.shortcuts.
render() has the following arguments:
-
request- The HTTP request object. -
template_name- The path to the template file that will be included in the HTTP response. -
context- Dict of variables that are accessible from within the template. -
content_type- The MIME type of the returned document, you do not need to include this. -
status- The HTTP status code for the response, this defaults to 200. If you want to return an error like404there are better functions to use. -
using- The name of the template engine, this won't be necessary for the portfolio project.
The primary purpose of a view is to build the context that will be rendered with the template, as well as choosing which template to use.
If the page the user requested is supposed to display details about an object, then that object should be included in the context.
The context is just a python dict object. You can write it like context = {}.
You can access and add new elements to a dict by using brackets: context["key"] = value.
You can also define the context in in one line by assigning key-value pairs within the {}: context = {"key":value}
To return errors, you can use the HTTP error classes from django.http.
For example:
from django.http import HttpResponseNotFound
def index(request):
return HttpResponseNotFound("<h1>Page not found</h1>")
This will return a 404 page with only the text "Page not found" when called.
You can redirect the client to a new URL by using the redirect() function in django.shortcuts.
This will return an HttpResponse object that you can return from your view that will instruct the client browser to go to a new URL.
For example, redirect('/students') will redirect to the path /students. Any URL will work in this area however.
Model objects can be accessed from within a view function using the QuerySet API. For example, Student.objects.all() will return all Student objects.
You can use other features of the QuerySet API if you want to only access specific objects.
If you make modifications to Model Objects within the view function, you need to call the save() method on that object. For example:
portfolio.active = False
portfolio.save()
You can create objects in the view by using the Model.objects.create() function. For example:
project = Project.objects.create(**kwargs)
project.save()
URL parameters, which is any text that occurs after a ? in the URL can be obtained by accessing the body of the HttpRequest object.
For GET requests, you use request.GET.get('paramName', defaultValue)
The paramName simply refers to the part of the parameter before the =. defaultValue is the value that this function returns if the parameter is not present in the URL.
If your URL is formatted like http://localhost:8000/student?id=1 then the function request.GET.get('id', False) will return '1', if ?id=1 is not present in the URL, it will return False
You can access many features of the HttpRequest including the HTTP method, the request body, the cookies, the headers, and all the parameters.