Django DeleteView with AJAX

The default Django generic DeleteView is not perfectly adapted for AJAX requests. A much simpler AjaxDeleteView can easily be implemented using the same mixins as Django’s generic class-based views: class AjaxDeleteView(SingleObjectMixin, View): “”” Works like DeleteView, but without confirmation screens or a success_url. “”” def post(self, *args, **kwargs): self.object = self.get_object() self.object.delete() return HttpResponse(status=204) This […]

Return a user’s own objects with Django’s class-based views

There is a very simple way to only return the connected user’s own objects with Django’s generic class-based views. When you extend get_queryset() on any view that implements SingleObjectMixin or MultipleObjectMixin (almost all of them), you can filter the default QuerySet to match your needs. It becomes fairly easy to create a mixin that filters […]

Pagination with Django’s generic ListView

Did you know the Django ListView supports pagination out of the box? All you need to do is specify the number of items per page with the paginate_by attribute: class ArticleList(ListView): model = Article paginate_by = 10 The queryset available in object_list will be paginated, so you will only get 10 results. page_obj and paginator […]

Telling if a field is required in Django templates

If you want to tell whether a field in a template is required, use form.myfield.field.required as in the example below: {% for field in form %} <label for=”{{ field.id_for_label }}”> {{ field.label }} {% if field.field.required %}*{% endif %} </label> {{ field }} {{ field.errors }} {% endfor %} In this example, there will be […]

Following redirects with the Django test client

When running unit tests with in Django, the test client’s default behaviour is to stop at the first response, even if that response is a redirect. If you want the client to follow these redirects and return the last page, perform your requests like this: response = c.get(‘/redirect_me/’, follow=True) This will also add response.redirect_chain so […]

Having issues with your first Django deployment on Heroku?

After spending a part of the night swearing at Heroku for not seeing my Django app correctly, I have realized it requires a very specific folder structure to operate properly. In my case, my whole Django project was in a subdirectory of my git repo (git_root > django_project > django_project >, while Heroku expects […]

How to fix long page load times on Heroku

If you have a free Heroku instance, you might sometimes face a long first page load time on your web application. This is because Heroku will shut down your instance after a while if nobody visits your site. This is a problem I have encountered with Bill Splitter since my girlfriend and I were the […]

Tastypie: only allow a user to use its own resources

If you want to limit user access to the resources they own in Tastypie, define obj_create and apply_authorization_limits as such. This will automatically assign created items to the current user, and only return resources that belong to the current user. from tastypie.resources import ModelResource from notes.models import Note class NoteResource(ModelResource): class Meta: queryset = Note.objects.all() […]

How to redo a South migration after using –fake

If you are using Django South, you might accidentally use migrate [app] –fake at some point. To undo your mistake, use the following commands: python shell from south.models import MigrationHistory m = MigrationHistory.objects.get(migration=”<migration_name>”) m.delete() That’s it! You can now safely perform your migration again.

How to clone model instances in Django

Let’s say you want to create an exact copy of your an object instance. All you need to do is set the primary key to None and save the object again. object_copy = MyObject.objects.get(pk=…) = None Tada! You now have a copy of your object with a new primary key.