Django Shortcuts
Django shortcuts module is a collection of helper functions that are generally used in view function/classes. There are many shortcuts available in module django.shortcuts. In other words, these function /classes introduce controlled coupling for convenience’s sake.
render()
It combines a given template with a dictionary and returns the HttpResponse object with that rendered text. Following is the syntax of the render() function.
Syntax –
Parameters –
Below are the parameters of render() function.
- request – It is used to generate the response.
- template_name – It takes the template names and display the template contents.
Optional Parameters –
- context – It represents the dictionary of values to add to the template context.
- content_type – The MIME type to use for the resulting document. Default to ‘text/html’.
- status – It shows the status code for the response. Defaults to 200.
- using – The name of a template engine to use for loading the template.
Example – In the following example, we render the template newapp/index.html.
It is equivalent to the below code.
redirect()
The redirect() function is used to redirect to the specific URL. It returns an HttpResponseRedirect to the appropriate URL for the argument passed. Let’s see the following syntax.
Syntax –
Parameters –
- A model: The model’s get_absolute_url() function will be called.
- A view name with arguments: urls.reverse() will be used to reverse-resolve the name.
- A URL will be used as-is for the redirect location.
Example –
By default, the redirect() returns a temporary redirect. However, we can returns the permanent redirect if set to True.
get_object_or_404()
It returns the DoesNotExist exception if the searched object is not found. On the other hand, get() method raise Http404.
Parameters
- Klass – A Model class, a Manager, or a QuerySet instance from which to get the object.
- **kwargs – Lookup parameters, which should be in the format accepted by get() and filter().
Let’s understand the below example.
Example –
It is equivalent to:
get_list_or_404()
It returns the results of filter() on a given model manager cast to a list, raising Http404 if the resulting list is empty. The syntax is same as get_object_or_404.
Parameters
- klass – A Model, Manager or QuerySet instance from which to get the list.
- **kwargs – Lookup parameters, which should be in the format accepted by get() and filter().
Let’s understand the following example.
Example –
It is equivalent to:
We have discussed a few important shortcuts that provide control over the objects. These shortcuts also allow handling the potential errors effectively.