djangosnippets.org: Latest snippets tagged with 'login'https://djangosnippets.org/tags/login/2015-07-09T14:46:33.820390-05:00@group_required decorator
2015-07-09T14:46:33.820390-05:00sebastien247https://djangosnippets.org/snippets/10508/<p>Use :</p>
<p><code>@group_required(('toto', 'titi'))</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p><code>@group_required('toto')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>Note that group_required() also takes an optional login_url parameter
<code>@group_required('toto', login_url='/loginpage/')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>As in the login_required() decorator, login_url defaults to settings.LOGIN_URL.</p>
<p>If the raise_exception parameter is given, the decorator will raise PermissionDenied, prompting the 403 (HTTP Forbidden) …</p>
Freely redistributableUse django-social-auth & Google Accounts for admin login
2012-12-04T09:27:39.814163-06:00pmdarrowhttps://djangosnippets.org/snippets/2856/<ol>
<li>Create an app and place this in <code>admin.py</code>. </li>
<li>Add <code>url(r'^login/$', 'social_auth.views.auth', {'backend': 'google'}, name='login')</code> to your <code>urls.py</code>.</li>
<li>Add the app to your <code>INSTALLED_APPS</code> after <code>django.contrib.admin</code>. </li>
<li>Set <code>USE_SOCIAL_AUTH_AS_ADMIN_LOGIN = True</code> in your <code>settings.py</code>.</li>
<li>...</li>
<li>Profit.</li>
</ol>
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 redistributableShow logged users - keeping track of users login and logout
2012-09-05T15:02:18.391108-05:00albertorcfhttps://djangosnippets.org/snippets/2808/<p>Showing a list of logged users using the <em>user_logged_in</em> and <em>user_logged_out</em> signals.</p>
<p>See <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#login-and-logout-signals">login and logout signals</a> in Django docs.</p>
Freely redistributableDjango plugin for LoginRadius
2012-02-10T13:40:06.186600-06:00diegolishttps://djangosnippets.org/snippets/2681/<p>Dependencies: urllib, simplejson</p>
Freely redistributableDjango Sudo
2011-12-14T13:53:07.635320-06:00readevalprinthttps://djangosnippets.org/snippets/2628/<p>Staff can log in as a user, from a url to help with customer support or debugging.</p>
Freely redistributableDecorator for authenticating token based API calls
2011-01-07T06:50:04.223280-06:00jpulgarinhttps://djangosnippets.org/snippets/2313/<p>Uses the token generator located at django.contrib.auth.tokens as an authentication mechanism aimed mainly at API calls. Any POST request with a valid token and user parameter will work as if the user were logged in normally.</p>
Freely redistributableFacebook Authentication Backend
2010-06-15T19:42:54.859443-05:00barnardohttps://djangosnippets.org/snippets/2065/<p>Authentication through Facebook's Graph API. See</p>
<p><a href="http://developers.facebook.com/docs/authentication/">http://developers.facebook.com/docs/authentication/</a>
<a href="http://developers.facebook.com/docs/authentication/permissions">http://developers.facebook.com/docs/authentication/permissions</a>
<a href="http://developers.facebook.com/docs/api">http://developers.facebook.com/docs/api</a>
<a href="http://github.com/facebook/python-sdk/blob/master/examples/oauth/facebookoauth.py">http://github.com/facebook/python-sdk/blob/master/examples/oauth/facebookoauth.py</a></p>
<p>Define the facebook tokens in settings.py and replace <app_name> with the name of your app. You will probably want to modify the scope on the authorize link in the template, see the authentication permissions link.</app_name></p>
<p>This updates the user model …</p>
Freely redistributablelogin on activation with django-registration
2010-03-15T22:47:59.883104-05:00morganhttps://djangosnippets.org/snippets/1960/<p>Here's a signal handler to log a user in on registration activation. It took me an hour to figure out that I needed to put the user.backend in quotes and google wasn't being my friend.</p>
<p>from <a href="http://docs.b-list.org/django-registration/0.8/faq.html">the django-registration documentation</a>:
How do I log a user in immediately after registration …</p>
Freely redistributableRemember me for login
2010-01-22T07:54:03.091007-06:00iElectrichttps://djangosnippets.org/snippets/1881/<p>Set session to never expire in settings, and when remember_me is found false in login POST, set it to browser session expiry. Works only in Django 1+.</p>
Freely redistributableLogin as Any Site user
2009-11-29T19:15:01.468760-06:00madhav.bnkhttps://djangosnippets.org/snippets/1822/<p>If you are an admin of a django site and many times it feels to visualize or experience how a normal website user actually sees different pages of your website. So to get this done, we create a normal user (just for testing purposes) and check the same, which is …</p>
Freely redistributableOracleAuthBackend
2009-08-10T14:14:18.892775-05:00nosrednakramhttps://djangosnippets.org/snippets/1678/<p>This code uses oracle as an authentication back end. It creates a new connection to the db and attempts to login. If successful it will then create an upper case User account with _ORACLE appended to the username.</p>
<p>My urls.py call:</p>
<pre><code>from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^accounts/login/$', 'django.contrib.auth.views.login', …</code></pre>
Freely redistributableEmail or username authentication with masquerading
2009-06-23T17:17:23.645605-05:00petrillihttps://djangosnippets.org/snippets/1590/<p>This backend will allow you to have users login using either their username or the email address as it is in the User model. In addition, it will allow anyone with the staff priveleges to login as another user. The method is to user the user you wish to masquerade …</p>
Freely redistributableLoginAsForm - Login as any User without a password
2009-06-04T15:53:57.354206-05:00johnboxallhttps://djangosnippets.org/snippets/1552/<p>Sometimes the only way to reproduce a bug on a production site is to login as the User who encountered it. This form allows you to login as any user on the site.</p>
<p><strong>Usage</strong></p>
<pre><code>@staff_member_required
def login_as(request, template="login_as.html"):
data = request.POST or None
form = LoginAsForm(data, request=request)
if form.is_valid()
form.save() …</code></pre>
Freely redistributableLog in a user without requiring credentials
2009-06-02T17:10:27.574816-05:00SmileyChrishttps://djangosnippets.org/snippets/1547/<p>Rather than requiring a dummy backend (which I have seen some people do), use this method to log in a user without requiring their credentials.
</p>
Freely redistributable