Chapter 8: Advanced Views and URLconfs - 0 views
-
Here, each view starts by checking that request.user is authenticated — that is, the current user has successfully logged into the site — and redirects to /accounts/login/ if not.
-
It would be nice if we could remove that bit of repetitive code from each of these views and just mark them as requiring authentication.
-
Now, we can remove the if not request.user.is_authenticated() checks from our views and simply wrap them with requires_login in our URLconf:
- ...3 more annotations...
-
This has the same effect as before, but with less code redundancy. Now we’ve created a nice, generic function — requires_login() that we can wrap around any view in order to make it require login.
-
There’s an important gotcha here: the regular expressions in this example that point to an include() do not have a $ (end-of-string match character) but do include a trailing slash.