Login

Snippets by ludvig.ericson

Snippet List

Serving null files

Sometimes you have to serve null files. This quick hacky generic view lets you do that. The best example of that is robots.txt, I want my robots.txt to always be empty, at least for now. The other example would be favicon.ico, but that's more often used to actually fill a purpose.

  • null
  • files
  • views
Read More

Line & paragraph chopping

I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic") Use like so: {% load myExtraModule %} {{ blogpost.content|paragraphs:"2" }} The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.

  • chop
  • cut
  • line
  • paragraph
  • block
Read More

Django & cache headers

This takes advantage of a recently added feature to django, being able to give it real functions as the view instead of having it be a string that is has to look up itself. It takes advantage of how decorators work and how `cache_control` works, normally you'd do something like this: @cache_control(private=True, public=False) def view_stuff(request): # ... return response Which is equal to doing `view_stuff = cache_control(private=True, public=False)(view_stuff)` after definition. `cache_control` is a function factory, more or less, which we use to our advantage.

  • cache
  • decorators
Read More

Debug-only static serving

A simple way to enable static serving while in development stage still - on release, simply set up the web server to serve the static content instead, and adjust `MEDIA_URL` accordingly.

  • static-serve
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More