How to fix ImportMismatchError in Python

If you get an ImportMismatchError when running a Python app, it’s likely that you have some Python bytecode files (*.pyc) from a different runtime. For example, this happens when I run my Python unit tests inside a Docker container, then try to run them again in PyCharm. Here is an example error message: /…/env/bin/python2.7 “/Applications/PyCharm […]

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 […]

A heap implementation in Python

One more python implementation of a data structure. This is a heap implementation that follows an architecture similar to the binary search tree we have built earlier. from collections import Container class Heap(Container): array = [] # Allows “if item in tree:” def __contains__(self, item): return self._contains(item, 1) def _contains(self, item, index): if item > […]

A binary search tree in Python

As part of my series on concepts you should never, ever need to reimplement in a real world scenario, here is a binary search tree in Python. This particular example contains a bit of duplicate code, but I doubt the benefits of changing this would outweigh the loss in readability, especially for a learning example. […]

A bubble sort in Python

As a second exercise in preparation for my Microsoft phone interview, I give you the Python bubble sort (or sinking sort). Once again, this is just an exercise. You should use the standard Python facilities for your sorting needs, especially given the terrible efficiency of bubble sort. from random import shuffle countries = ["Afghanistan","Albania","Algeria", ...] […]

A binary search in Python

This week, I received some exciting news by email: I will be interviewed for an internship at Microsoft! In order to keep my memory fresh, I have decided to practice my algorithms and data structures, then post the results. To get things started, here is how you perform a binary search in Python, not that […]

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 […]