Sydney Django Meetup Recap Sept 2014

Hey all, thanks for the great meetup! An additional thanks to the IC who kindly hosted the event and provided pizza and beer. I've thrown this together for the attendees as we collectively pooled a lot of great questions with some resources to resolve them (or at least point us in the right direction!).

The meetup consisted of a Q&A round-robin in which any attendee could throw up their hand and ask about roadblocks they're running in to. The questions and answers are collected approximately in the order I recall; and if I mentioned something but forgot to write it down just email me [mattoc @ this domain] or tweet me @mattoc.

Command-line scripts

There was a discussion on running custom python shell scripts to run tasks such as screen scraping with the requests [http://docs.python-requests.org/en/latest/] and BeautifulSoup [http://www.crummy.com/software/BeautifulSoup/] libraries. Apparently the Django 1.7 upgrade changed some environment handling; possibly due to the new app loading system.

It was recommended that they take a look at Django's custom management commands [https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/] as they're already wrapped in the project context. Existing scripts can be trivially ported to the handle method on a Command or NoArgsCommand subclass.

How to find Django apps

A couple of people raised the difficulty in locating an appropriate Django package to solve a particular problem. Django Packages [https://www.djangopackages.com/] was suggested to assist as it categorises open-source apps and lists: pertinent feature implementations, repository activity, stability and a whole host more useful metrics.

We've all run in to what appears to be the de facto app but was unmaintained and didn't work with the stable version of Django; this site should help narrow down leads. The Python Package Index (PyPI) [https://pypi.python.org/pypi/] is also a great resource -- you may not need a Django-specific package to solve your problem!

How to find Open Source projects to contribute to

One of the attendees wanted to get involved in some community projects but didn't know where to start looking. It was suggested they take a look at:
Additionally:
  • Browse or raise your hand on /r/django or /r/python
  • Lurk on #django
  • Subscribe to django-users

Template hierarchy

An attendee got stuck on some path issues on Windows--specifically for template loading. The docs for Django's template loader are here https://docs.djangoproject.com/en/1.7/ref/templates/api/#loading-templates and they should cover most bases. We did a quick whiteboard sketch to show the cascade of templates and why and how the paths are specified for the {% extend <path> %} template tag. Django Debug Toolbar [https://pypi.python.org/pypi/django-debug-toolbar] could help shine some light on the template inheritance chain.

Forms & ModelForms

A couple of questions about forms were raised:
  • How to know whether to use the lower-level Form or ModelForm API
  • How to customise the render of a form in a template

We recommended prototyping with a ModelForm and using its customisability to change the resulting render, with a standard Form if finer-grained control is required. Something of note is that people were getting stuck not knowing about certain options like fieldset so we recommended keeping both the Forms user guide and Forms API tabs open.

As for rendering you can approach it by hand, or iterating over the {{ form.fields }} and using a helper library such as django-widget-tweaks [https://pypi.python.org/pypi/django-widget-tweaks] or let a framework handle it for you; django-crispy-forms [http://django-crispy-forms.readthedocs.org/en/latest/] - bonus - it renders forms with Boostrap 3 automagically.

REST frameworks

Simple solution: return a JSON response from your view (either with json.dumps or my personal favourite: jsonview [https://github.com/jsocol/django-jsonview])

1
2
3
4
5
6
7
from jsonview.decorators import json_view

@json_view
def all_todo_items(request):
   return {
       TodoItems.objects.all(),
   }

Ideally one would use django-rest-framework [http://www.django-rest-framework.org/] for anything requiring heavy lifting. The recent US DjangoCon '14 had a presentation on this topic by Nina Zakharenko.

Python 2 vs 3

Big question, but a unanimous answer: if you're starting out with a new project go with Python 3 unless there's an absolutely crucial app that is only Python 2 compatible.

Visualising Django DB Schema

There's a few alternatives to simply poking around with psql:

1
$ python manage.py graph_models -a -o myapp_models.png

Database scaffolding

By using manage.py inspectdb you can generate models for existing schema. See https://docs.djangoproject.com/en/1.7/howto/legacy-databases/ for a HOWTO.

Must-haves

Django Debug Toolbar https://pypi.python.org/pypi/django-debug-toolbar Django Extensions shell-plus + IPython: http://ipython.org

...and that about wraps it up. Again; please hit me up if I've left something out or you want to share a better way!