Categories
Javascript Python

Django REST Framework & AngularJS $resource – Trailing Slash Problem

If you use ngResource ($resource) from AngularJS, you probably have already noticed that AngularJS removes the trailing slash from an URL. Django, on the other hand by default has “APPEND_SLASH” turned on. So when you request:

http://example.com/api/object

Django redirects you to:

http://example.com/api/object/

Yes, Django adds an extra slash to the URL. Django REST framework also follows this scheme. Every api end point has a trailing slash.

Angular, not having any trailing slash will always request the first URL above and get redirected to the second one. Apart from the cost of one extra http request, this is fine. The pain appears when we want to make POST request. Django can’t redirect POST requests and won’t accept POST data on the URL without a slash at the end. Damn, Our AngularJS app breaks because it can’t make request to the URL Django expects!

This is a very annoying and known bug with an open issue on their Github for quite sometimes. Since the latest version doesn’t yet have it fixed, I was stuck. Then I noticed somewhere that Django REST Framework responds to this particular bug allowing an extra parameter to “DefaultRouter” to create API endpoints without any trailing slash. Just do this –

It will make sure that the API urls don’t have any slash at the end and play nice with Angular. But we’re not done yet, Django will still try to redirect requests appending a slash. Let’s fix this by adding this to the main settings.py:

Now, we should be able to make REST calls using AngularJS and $resource with sanity! 🙂

Categories
Python

Django on Heroku: Postgres and dbshell

If you’re deploying a Django app on Heroku you should already know that Heroku uses Postgres. The surprise comes when your “dbshell” command will fail with an error message like:

You appear not to have the ‘psql’ program installed or on your path.

The workaround is rather simple, you have to copy the SQL manually and run it from the heroku pgsql console. To run the console, type in –

And you should get a prompt to type in your pgsql queries. Type in your SQL and commit. It should work! 🙂

Categories
Mac Python

Python: Sending Growl Notifications

Growl is an extremely popular notification system for OS X. The application allows developers to display beautiful notification messages with ease. If you’re developing an application in Python and would like to integrate with Growl, this blog post is for you.

Introducing GNTP

Let me quote from the official docs here – http://www.growlforwindows.com/gfw/help/gntp.aspx#intro:

The Growl Network Transport Protocol, or GNTP, is a protocol to allow two-way communication between applications and centralized notification systems such as Growl for Mac OS X and to allow two-way communication between two machines running centralized notification systems for notification forwarding purposes.

In short, GNTP allows two way communication between your application and Growl. Even shorter – you can use GNTP to publish notifications using Growl.

GNTP Binding for Python

We have an excellent library for interacting with GNTP. The project is hosted on Github – https://github.com/kfdm/gntp/ and the docs are available here – http://pythonhosted.org/gntp/

So how to use this library? First, install the library using pip –

To see if the installation was ok, we can test by running the module directly –

Sending Notifications

Sending a quick notification from the Python interactive prompt:

Simple, right?

You can find advanced examples on the github readme and the docs (links above). Have fun with Python and Growl!