Now that you can log users in and out, you can start securing your website by verifying that users are authenticated before giving them access to your views. You can verify authentication in both your view functions and template files.
You can use the is_authenticated() function of the User object in a view's HttpRequest to determine if the user is authenticated. Then you can modify the view's behavior, depending on whether the user is authenticated. For example, if the user isn't authenticated, you could redirect that person to a login page, render a different view, or render only a portion of the current view.
The following code snippet shows an example of using the is_authenticated() function in a view to redirect unauthenticated users to a login page:
def secure_view(request): if not request.users.is_authenticated(): return HttpResponseRedirect('/Login')
Django provides a decorator function that simplifies verification of authentication. The login_required() function verifies whether the user is authenticated. If the user isn't authenticated, this function redirects the user to the URL specified in the LOGIN_URL setting in the settings.py file. The login_required() function passes the URL of the current request to the login function as a query string using the next variable name. For example, the following code applies the login_required decorator:
@login_required(redirect_field_name='reDir') def secure_view(request): . . .
If the user is not authenticated, this decorator redirects the browser to the following URL, where LOGIN_URL is set to /Login and the request.get_full_path() for the secure_view() view function is /views/secure_view:
/Login?next=/views/secure_view
Did you Know?
The login_required() function also accepts an optional argument, redirect_field_name. It allows you to specify the argument name, instead of next, that should be used to pass the redirection URL to your login function:
@login_required(redirect_field_name='reDir') def secure_view(request):
You can also verify whether the user has been authenticated in a template by passing in a RequestContext and then accessing the User object. Then you can use the authentication information to determine how to render the template.
For example, the following code snippet verifies whether the user is authenticated before showing a table of data:
{% if user.is_authenticated %} <table> {{ secure_data }} </table> {% endif %}
Try It Yourself: Verify Authentication in Templates and ViewsIn this section, you will modify the base template to display a welcome message if the user is authenticated. You will also add a decorator template to the People details() view function to force users to be authenticated before they view the home page. You will also modify the home_view() function to verify authentication and pass an empty Blog list if the user isn't authenticated so that no blog entries appear on the home page.
Listing 15.4. Imports and the details() View Function of the iFriends/People/views.py File
Listing 15.5. Imports and the home_view() Function of the iFriends/Home/views.py File
Listing 15.6. Table Row Containing the Navigation Link and Login/Welcome Data in the iFriends/templates/iFriends_base.html File
|