Without the right set of tools by your side, it’s hard to get the job done quickly and effectively. When it comes to Django application development, there are many tools to pick from. When I develop in Django, my tool of choice is PyCharm, a feature-rich IDE. With PyCharm, a developer can rapidly code and debug applications. As a craftsman can be known by the tools he uses in his shop, so too can a developer.
Without fail, every developer passionately defends their particular toolset. Among the Django team at Concentric Sky, Sublime Text and Vim are popular choices. PyCharm is most valuable because of the powerful debugging, code navigation, and code completion tools it provides to the developer.
It's hard to beat PyCharm’s efficiency and clarity as an IDE debugger. You can put a breakpoint on a line, refresh the page in question, see the current variables that are in-scope, and step through the code line-by-line to find where your code breaks down. With consideration of the time developers fritter away debugging, any opportunity to introduce efficiencies or improvements are critical.
One of my favorite ways to use PyCharm’s debugger is to put a breakpoint on a line that I can't get quite right. Before the process executes that line, I copy-paste it into PyCharm’s console and execute there, in a separate thread. What this does is allow me to see the results and easily adjust the line until it does what I want. With the console I can run the line in the exact context the page would. This is quicker than repeatedly adjusting the line, refreshing the code and reviewing the output.
The good old import pdb; pdb.set_trace()line to set a "breakpoint" just doesn't cut it for me. PDB is like shining a little key-chain flashlight into a dark room, while PyCharm's debugger is like flipping the light-switch.
While the Django documentation provides a pretty good view, sometimes you need a clearer picture. Often, to understand exactly what your code is doing, you dig into Django core, or other libraries. You could open up contrib.auth on GitHub, but that takes you out of context and does not help you to find the exact file, class, and method that you're looking for.
PyCharm makes instant click-to-find navigation possible.
Let's say you write a view that subclasses UpdateView you override the form_valid() method to add some extra post-save logic and you can't remember where the redirect happens or how it knows which URL to redirect to. What do you do? Simple press Command+U (on OS X) and up will pop ModelFormMixin that shows form.save() is called. Press Command+U again and you'll jump to FormMixin returns a redirect to self.get_success_url()? Want to know what happens there? Just Command+Click on the line -- PyCharm will jump to it.
And getting back to where you were before is a cinch. Just like a browser’s forward and back buttons, you can use Command+[ and Command+] to trace your history back to the class you started in.
Or, if you know what class you want to look at -- but it has no relation to what you're currently looking at -- just use Command+O to open a nifty auto-complete dialog that lists every class in your project (and any libraries that you're using). You can also jump to a file with Command+Shift+O; alternatively you can jump to a symbol or function with Command+Option+O.
If you use these shortcuts, you can dash around your project. Of course you could do something like this without PyCharm. However, it would take you more time, leading to you being a less efficient and productive developer.
Let’s backup to the UpdateView example. We want to add some variables to the context_data in the template. You could type out the code, call to super() and try to remember the method signature. But PyCharm provides a better way to do this -- press Command+N, type in "get_context_data", press enter, and PyCharm will generate this code block:
def get_context_data(self, **kwargs): return super(PersonUpdateView, self).get_context_data(**kwargs)
Just start hacking away to insert your own variables.
PyCharm also provides hinting about method signatures. The hinting allows you to quickly remember the needed arguments to pass. If you’re like me, you don’t want to worry about typing out all the appropriate imports -- just press Command+Option on a class and PyCharm will add it to your imports.
Eachb time you start a new Django project in PyCharm, you’ll specify settings wield PyCharm's power. Here's a quick list of settings to check on:
- Django Support > Specify your code root, settings.py, and manage.py locations
- Project Interpreter > Configure Interpreters If you're using virtualenv (and you should be) for each project, you'll need to add the location of the Python executable. PyCharm’s knowledge of the executable location tells it about all the libraries you've installed with pip.
- Project Structure: If you have any extra paths that need to be in your PYTHONPATH for this project, add them as a "Source Folder;" and if you have any folders that contain code or files you don't want PyCharm to scan and index (like big media directories) then add them as an "Excluded Folder".
Everyone has tools that work best for them, but PyCharm's features are pretty hard to beat even for the most adamant of Vim-lovers.
comments powered by Disqus