Tag: django

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

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