Technology for ponies

Mar 25, 2014

Use HTTP basic authentification to login into Django

Let's imagine you have a view somewhere on your website you want to password protect using your usual django login but you are too lazy to design a form to input your credentials. This makes sense if the view is for your own use and you don't need to have a fancy login page. In such a case the easiest and fastest way to proceed is to use the standard HTTP basic authentification to let your browser asks for your credentials.

It is then easy to get the user / password back in your view and to authenticate yourself into django. Here is the code snippet to do this :

from django.http import HttpResponse
from django.contrib.auth import authenticate
import base64

def my_view(request):
      if 'HTTP_AUTHORIZATION' in request.META:
              auth = request.META['HTTP_AUTHORIZATION'].split()
              if len(auth) == 2:
                      if auth[0].lower() == "basic":
                              username, password = base64.b64decode(auth[1]).split(':', 1)
                              user = authenticate(username=username, password=password)
                              if user is not None and user.is_staff:
                                      # handle your view here
                                      return render_to_response('my_template.html')

      # otherwise ask for authentification
      response = HttpResponse("")
      response.status_code = 401
      response['WWW-Authenticate'] = 'Basic realm="restricted area"'
      return response

If you need to protect more than one view you should wrap this code in a view decorator.

Please not that using HTTP basic authentification your username and password are sent base64 encoded but as it can be easily decoded you should have your website served over https to keep your crendentials secured.

Dec 17, 2013

Admin interface with foreign key on a very long list

If your application has a model with a ForeignKey on an other model which gets a lot of entries in the database, the auto-generated admin interface can become a nightmare to load. Django will render your ForeignKey using a select drop-down and with thousands of entries loading time and browser memory are in bad shape.

Solution is rather simple, add your ForeignKey in the raw_id_fields parameter in the Django admin. Lets consider this example :


class MyModel(models.Model):
  user = models.ForeignKey(User)


from models.py import MyModel

class MyModelAdmin(admin.ModelAdmin):
    raw_id_fields = ("user",)

admin.site.register(MyModel, MyModelAdmin)

Jun 17, 2013

Check all items in a list are contained in an other list

In some code I wrote today I needed a way to check that all items in a list are contained in an other list. And I came up with a one-liner I found useful enough for sharing :

>>> big_list = [1,2,3]
>>> small_list = [1,3]
>>> all([i in big_list for i in small_list])
>>> small_list = [1,4]
>>> all([i in big_list for i in small_list])
Next → Page 1 of 2