Django ShortcutsDjango 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.
Optional Parameters -
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 -
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
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
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.
Next TopicHow to connect MySQL to Django
|