Python and Django

12 Feb, 14

Start Django Project With Two Scoops of Django Template

Run this line in order to start Django project by using Two Scoops of Django Project Template:

1
  1. django-admin.py startproject --template=https://github.com/twoscoops/django-twoscoops-project/zipball/master --extension=py,rst,html PROJECT_NAME
Read now
11 Feb, 14

Russian Slugs for Django Taggit

It appears that Django Taggit doesn't support transliteratable tags, what we need to do is to add such support, how you may ask? We should use proxy models. Just add this piece of code to, let's say, ru_taggit.py file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10 ...
Read now
06 Feb, 14

Sorl-Thumbnail returns error: "SuspiciousOperation: Attempted access to '/media/products/505.jpg' denied"

When Solr Thumbnail (https://github.com/mariocesar/sorl-thumbnail) returns and error like "SuspiciousOperation: Attempted access to '/media/products/505.jpg' denied" or Bad request: 400 you need to check the path of the image you want to resize: it shouldn't start with your MEDIA_ROOT like  '/media/products/505 ...

Read now
09 Nov, 13

Debugging Python Code

Debugging python code is extremelly simple and powerfull. Just use ipdb for this purposes:

1
  1. pip install ipdb

Then open up your code and in the place where you need code to stop insert this:

1
  1. import pdb; pdb.set_trace()

When you refresh the browser or execute a .py file ...

Read now
07 Nov, 13

Drupal meets Fabric: Deploying Drupal and Performing Different Admin Related Tasks with Fabric

Yesterday, during a meeting with my Drupal friends, I showed them some Python magic including Fabric () and how it can be used to make your Drupal dev life easier. My friends been blown away ;) I promised to share my regular fabfile.py for a Drupal project, please see it below ...

Read now
31 Oct, 13

About creating related objects dynamically on object save

When you save your main object and want to attach / create / modify attached to it related models you need to remember the following:

1) Do this in post_save signal, because related objects need pk and you get pk in post_save (when you create a new object)

2) If you manipulate ...

Read now
30 Oct, 13

Many-to-many field, save() method and the Django admin

Surprise surprise! When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is cleared and the new values set to whatever came out of the form. So if you are in ...

Read now
09 Oct, 13

How To Put A Page Under Active Menu Item with Django Sitetree

Django sitetree is a great app to manage your Django app menus. Sometimes, you'll have a page that you want to manually put under a sitetree, so how to do that? At first we need to patch a sitetreeapp.py like this:

 1
 2
 3
 4
 5
 6
 7
 8 ...
Read now
09 Oct, 13

Compare two date-times

To compare two times you need to do the following:

1
2
3
4
5
6
7
  1. from datetime import datetime,timedelta
  2. d = datetime.now()
  3. d1 = d+timedelta(minutes=20)
  4. if d < d1:
  5. print 'Hello'

When you compare django's DateTimeField value with timedelta you can get an error: "can't ...

Read now
08 Oct, 13

Perform action on model delete

Surprise, surprise, when you want to perform some actions on model delete, overriding delete() method may be not enough, because when you do bulk delete of models via admin interface it won't touch model's delete() method. The way out of it is using signals:

1
2
3
  1. @receiver(post_delete, sender ...
Read now