Python and Django

03 Sep, 14

Make Django and AngularJS play nicely together

Been working a lot with AngularJS and Django recently. To make them play nicely together one of the first things you need to do in your Angular App is to enable CSRF support and send X-Requested-With header, do it like this:

1
2
3
4
5
6
  1. var app = angular ...
Read now
07 Jun, 14

Stop PIP from re-downloading same packages over and over again

PIP has a habit of re-downloading same packages every time you hit pip install package_name. That's not so cool, especially, when you deploy packages from requirements.txt and one of the packages failed to build, restarting pip install -r requirements.txt would lead pip to redownload all the packages again ...

Read now
01 Jun, 14

Cleanup Files (and Images) On Model Delete in Django

In Django, if your model has Filefield / Imagefield, when it's get deleted it doesn't delete attached files by default. To fix that you need to do two steps: 1) Add filecleanup function, that'll take care of deleting files attached to a model:

 1
 2
 3
 4
 5
 6
 7
 8 ...
Read now
29 Apr, 14

Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts

Very often we can have multiple Django apps each running celery and all this is installed on a single server. How do we isolate these separate app's celery workers? The answer is: Simple, if you use Rabbitmq as your broker. Rabbitmq lets us to add multiple virtualhosts, so we can ...

Read now
25 Apr, 14

ERROR: must be owner of extension plpgsql

Today while moving Django/PostgreSQL site between servers and importing an SQL dump into the new location I got a surprising error "ERROR: must be owner of extension plpgsql". I'm already used to PostgreSQL quirks so after a quick search I came to the following solution: Login as postgres user ...

Read now
23 Mar, 14

Pass request object to the form: CBV and Form Mixins

If you want to access data from request object in your model form these two mixins will help you to do it easily: 1) You'll need a View mixin like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  1. class RequestFormKwargsMixin(object):
  2. """
  3. CBV mixin which puts ...
Read now
23 Mar, 14

Sorting on chardfields in Haystack and Elasticsearch

Current version on Haystack (2.10) combined with Elasticsearch produces weird results when you want to sort search results on charfield, the issue has been reported here https://github.com/toastdriven/django-haystack/issues/866 and here: https://github.com/toastdriven/django-haystack/issues/569 Long story short, to fix the problem ...

Read now
06 Mar, 14

Haystack Search Pager

To make haystack search pager place nicelly with other get parameters, we can do something like this: 1) Create a templatetag:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  1. @register.simple_tag
  2. def get_search_pager_link(request):
  3. params = []
  4. link = '?'
  5. for key, value in request.GET.items():
  6. if key ...
Read now
28 Feb, 14

Django Messages and Bootstrap 3

To make Django messages look nice in your bootstrap 3 template you need two things:

1. Create a custom template tag to define css name for a message:

1
2
3
  1. @register.assignment_tag
  2. def get_bootstrap_alert_msg_css_name(tags):
  3. return 'danger' if tags == 'error' else tags

2. Add this snippet to your base ...

Read now
12 Feb, 14

Serve static and media files by Django while in Debug mode

Media and static files won't be served in devel mode untill you put this at the end of the main urls.py file:

1
2
3
4
5
  1. if settings.DEBUG:
  2. urlpatterns += patterns('',
  3. (r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
  4. (r'^static/(?P.*)$', 'django ...
Read now