Python and Django

06 May, 15

Running Django manage.py commands from anywhere

One of the most annoying aspects of developing in Django for me is running manage.py commands: you need to be in the same directory with manage.py file in order to run a command. This is not convenient. I thought it would be cool to be able to run ...

Read now
01 May, 15

Installing libsass (django_libsass) on Mac OS Yosemite

I started playing with wagtail recently (a very promisiing Django CMS) and they use django_libsass to compile their sass into css. Unfortunatelly, if you'll try to install django libsass via PIP on Mac you'll get a compile error message. I tried lots of approaches to fix this and it didn't ...

Read now
29 Apr, 15

Saving file field uploads not in MEDIA_ROOT directory

I had a requirement recently to upload files to some kind of a private directory, not accessible via www. Of course, I used Django model's FileField to solve the task, but the problem was in a fact that by default, FileField saves uploads under MEDIA_ROOT which is not what I ...

Read now
13 Mar, 15

Snippet: Auto select current user in Django admin form

1
2
3
4
5
6
  1. # file: models.py
  2. class Post(models.Model):
  3. title = models.CharField(max_length=255)
  4. content = models.TextField()
  5. date = models.DateField()
  6. author = models.ForeignKey(settings.AUTH_USER_MODEL)
Read now
06 Mar, 15

Snippet: Override field widget in Django admin form

1
2
3
4
5
6
  1. # file: admin.py
  2. class BlogAdmin(admin.ModelAdmin):
  3. formfield_overrides = {
  4. models.TextField: {'widget': CKEditorWidget},
  5. }
Read now
18 Feb, 15

Snippet: Django post_save signal on parent class with multi-table inheritance

1
2
3
  1. # Propagate signal across all children of a polymorphic parent.
  2. for subclass in Entity.__subclasses__():
  3. post_save.connect(delete_comments, sender=subclass)
Read now
21 Oct, 14

Django Uwsgi Nginx 504 Gateway Timeout

I had a long running process (data import) and encountered 504 Gateway Timeout error. I use Nginx / uwsgi pair to serve my Django app. After doing some research it turned out that the problem is in uwsgi_read_timeout directive. Directive sets the amount of time for upstream to wait for a ...

Read now
09 Oct, 14

AngularJS plus Django: A match made in heaven

I've just finished watching "AngularJS plus Django: A match made in heaven" DjangoCon US 2014 talk by Nina Zakharenko (Video | Slides) and it was quite helpful. Since I'm working with API's and mobile / angular apps a lot, I just made some helpful Django + AngularJS related notes that came out of ...

Read now
19 Sep, 14

Improving Number of Queries Generated by Django Comment's render_comment_list Template Tag

One of my Django apps uses Django Comments. To show a list of comments on a page I use this simple built-in template tag:

1
  1. {% render_comment_list for object %}

All is fine, but I noticed that it loads user object per every comment line. So if you have 50 comments on ...

Read now
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