Converting from Django to PHP