djangosnippets.org: Latest snippets tagged with 'login_required'https://djangosnippets.org/tags/login_required/2019-04-06T08:16:15.123452-05:00LoginRequiredMiddleware
2019-04-06T08:16:15.123452-05:00aasmprohttps://djangosnippets.org/snippets/10663/<h3>settings.py</h3>
<pre><code>MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'utils.LoginRequiredMiddleware',
]
LOGIN_REQUIRED_URLS = [
r'^panel/(.*)$'
]
</code></pre>
<p>this will help any url under <code>panel/</code> require login.</p>
Freely redistributableLogin Required Middleware with Next Parameter
2012-11-13T05:01:24.389430-06:00bernardoportohttps://djangosnippets.org/snippets/2845/<p>Based on <a href="http://djangosnippets.org/users/onecreativenerd/">onecreativenerd</a> code.</p>
<p>Sometimes it's a real pain to use the @login_required decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry …</p>
Freely redistributableTastypie v0.9.11 LoginRequiredAuthorization
2012-08-16T15:55:41.562047-05:00cottonhttps://djangosnippets.org/snippets/2801/<p>This is an Authorization class for <a href="http://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html">Tastypie</a> v0.9.11 (v0.9.12 changes how Authorization works).</p>
<p>DjangoAuthorization checks specific permissions — <code>add_model</code>, <code>change_model</code>, <code>delete_model</code>, etc. If you don't need that level of permissions checking, this might be useful. It just makes sure the User is logged in. It's equivalent to …</p>
Freely redistributableDecorate every view in a url tree
2011-11-18T16:50:54.972941-06:00sjzabelhttps://djangosnippets.org/snippets/2607/<p>Add login_required (or any other combination of decorators) to any view references by the urls created by patterns(...).</p>
<p>My personal little itch as an example...</p>
<p>urlpatterns += required(
login_required,
patterns('',
(r'^api/',
include(api.urls)),
)
)</p>
Freely redistributableLoginRequired class-based view decorator
2011-07-25T02:25:15.007512-05:00mjumbehttps://djangosnippets.org/snippets/2495/<p>Apply the <code>login_required</code> decorator to all the handlers in a class-based view that delegate to <code>cls.dispatch</code>.</p>
<p>Optional arguments:</p>
<ul>
<li>redirect_field_name = <code>REDIRECT_FIELD_NAME</code></li>
<li>login_url = <code>None</code></li>
</ul>
<p>See the documentation for the <a href="https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator"><code>login_required</code></a> method for more information about the keyword arguments.</p>
<p>Usage:</p>
<pre><code>@LoginRequired
class MyListView (ListView):
...
</code></pre>
Freely redistributablelogin_required decorator that doesn't redirect
2011-02-15T09:43:46.141655-06:00brutassehttps://djangosnippets.org/snippets/2357/<p>A login_required decorator that wraps the login view instead of redirecting to it.</p>
<p>This prevents your site from leaking login information with HTTP status codes as explained <a href="https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information">here</a>.</p>
<p>This is the way Django's admin is protected, the difference being that it checks for is_active and is_staff instead of is_authenticated. …</p>
Freely redistributableUse custom authentication backend with admin
2010-07-28T12:12:29.709835-05:00ungenio41https://djangosnippets.org/snippets/2127/<p>Forces admin site to use your custom login.</p>
<p>Very useful when using RemoteUserBackend.</p>
<p>See <a href="http://djangosnippets.org/snippets/2128/">here</a> for a use case.</p>
Freely redistributableSWFUpload auth decorator
2009-02-19T20:03:10.155466-06:00debricehttps://djangosnippets.org/snippets/1331/<p>I use this snippet to simplify my auth system with flash uploader SWFUpload. flash_login_required ensures that the user is authenticated and inject the context dictionnary into the specified template. To redirect a user, just set the variable <code>context['redirect']</code> with an url. </p>
<p>Remember to include the cookie js in your template …</p>
Freely redistributable