Login

Snippets by pterk

Snippet List

collectstatic for media folders

Note: This concerns django 1.3 but the tags does not yet exist. We have a couple of apps, including 3rd party apps, that have the 'static' files in 'media' dirs. These files aren't found with collectstatic in django 1.3. With this snippet they will be. To use it: * paste the code in a file e.g. yourproject/finders.py. * include the finder in settings.STATICFILES_FINDERS: STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'yourproject.finders.AppMediaDirectoriesFinder', ) * ./manage.py collectstatic -n -l

  • media
  • staticfiles
Read More

PreSaveMiddleware

With this middleware in place (add it to the MIDDLEWARE_CLASSES in your settings) you can pass a request to the model via a pre_save method on the model. I'm not sure if it is an improvement over the [threadlocals method] (http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) but it may be an alternative that can be improved upon? class MyModel(models.Model): name = models.CharField(max_length=50) created = models.DateTimeField() created_by = models.ForeignKey(User, null=True, blank=True) def pre_save(self, request): if not self.id: self.created_by = request.user

  • middleware
  • pre_save
  • signals
Read More

Basic HTTP Authentication

A patch (against django svn trunk [4649](http://code.djangoproject.com/browser/django/trunk/?rev=4649)) that allows users to log in with Basic HTTP Authentication i.s.o. login forms using some simple middleware (entire patch is ~50 lines). I was unaware of http://code.djangoproject.com/wiki/GenericAuthorization so I'm not sure about its usefulness in the long run. You can enable it by including 'django.contrib.auth.middleware.BasicAuthenticationMiddleware' in your MIDDLEWARE_CLASSES and then adding the following lines in your settings.py: BASIC_WWW_AUTHENTICATION = True WWW_AUTHENTICATION_REALM = "djangolures.com" Updated: See also http://code.djangoproject.com/ticket/3609 (patch now availble here as well).

  • middleware
  • django
  • http
  • basic
  • authentication
Read More

pterk has posted 4 snippets.