Getting Started with PyCharm as a Django Development Tool prof

Welcome to PyCharm! This Quick Start Guide aims to walk you through your first Django project with PyCharm. As an example, we'll take the polls application from Django — thus you will be able to try hand-on all the conveniences provided by PyCharm.

Before you start...

Make sure that you have Python properly installed on your machine.

Depending on the Django version, the following Python versions are supported:

  • Django 1.5 supports Python interpreters from the version 2.6 up to version 3.x.
  • Django 1.3 supports Python interpreters from the version 2.5 up to version 2.7.

Refer to the download and installation instructions for Python and Django.

Creating new project

Let's start from the very beginning, namely creating a Django application from scratch. Creating a new project is most easy: on the Welcome page, click New Project.

PyCharm's New Project wizard spares you from some tedious and error-prone work. PyCharm will create the proper Django application structure, provide the necessary import statements, add application to the list of installed applications, if necessary, uncomment lines for site administration and create a directory for the Django templates.

On the first page, enter project name and location, select the project type — Django application, and choose the Python interpreter to be used with your project. Note that the list of interpreters shows all previously configured interpreters. If you cannot find the desired one in the list, click the browse button, and configure something to meet your particular needs.

PyCharm project. Screenshot

By the way, if the Django framework is missing in the selected interpreter, PyCharm will help you installing it:

PyCharm project. Screenshot

Configuring virtual environment

Why virtual environment? Here we can list just a couple of situations, when it becomes really helpful:

  • Say, you are working with two applications, both requiring different versions of the same package. It is quite possible to install everything together, but in this case the required package will be upgraded.
  • You want to leave an application as is - you know for sure it works, and do not want to change the version of any of the required packages.

To make long story short, a virtual environment helps you keep versions under control. You can follow the installation and usage instructions, but PyCharm provides a handy way to define virtual environments right in the IDE.

  1. So, as mentioned above, click the browse button to open the Python Interpreters page. (Note that you can always invoke this page later, by clicking settings on the main toolbar.)
  2. In the Python Interpreters page, click venv:
    PyCharm project. Screenshot

    The dialog box Create Virtual Environment appears.

  3. Here you have to specify the name of the new virtual environment (let it be my_venv), its location, and, most important, the base interpreter to create your virtual environment for:
    PyCharm project. Screenshot

    Besides that, you can immediately make the new virtual environment the project interpreter by selecting the corresponding check box (if you don't do that at once, you can always do it later).

    PyCharm spends some time working on the new virtual environment, and in the meantime shows the progress bar:

    PyCharm project. Screenshot

    When the virtual environment is ready, you see all its paths and packages. So doing, PyCharm shows the currently installed and the latest available versions of each package. You can make a decision to upgrade.

    PyCharm project. Screenshot

By the way, it is also possible to create a virtual environment remotely, using Vagrant.

Click OK to create the project.

See also:
Configuring Python SDK
Configuring Interpreters with PyCharm

Creating your Django application

The stub project is ready, but it's still empty. Let's create a Django application:

  • Specify the application name. In our case, this name is polls. Note that you can create a project without an application, and add it later.
  • Specify the folder for the templates.
  • We also want to enable Django admin, and select the corresponding check box.
PyCharm project. Screenshot

PyCharm executes the startproject task of the utility, and produces the following structure:

PyCharm project. Screenshot

Configuring database

Now, when the project stub is ready, let's do some fine tuning. Open for editing the file. To do it, select the file in the Project tool window, and press F4F4. The file is opened in its own tab in the editor.

Specify which database you are going to use in your application. For this purpose, find the DATABASES variable: click Ctrl+F⌘F, and in the search field start typing the string you are looking for:

Find Text. Screenshot

  • In the 'ENGINE' line, add the name of your database management system after dot (you can use any one specified after comment, but for the beginning we'll start with sqlite3.)

    Note that you can use basic code completion ( Ctrl+Space⌃Space) here:

    Find Text. Screenshot
  • In the 'NAME' line, enter the name of the desired database, even though it doesn't yet exist.
    Database. Screenshot

Since we've prudently chosen sqlite3, we don't need to define the other values (user credentials, port and host). Let's now check whether our settings are correct. This can be done most easily — just launch the runserver task of the utility: press Ctrl+Alt+R⌥R, and enter task name in the pop-up frame:

Runserver. Screenshot

If everything is done properly, the console will show something like:

Runserver. Screenshot

Note that you can use the other databases. In this case, you will need to install the required database management systems and libraries, have the database running on your machine, and also specify your authentication parameters.

By the way, if you want to continue developing an existing Django application and see how nice is working on it in PyCharm, choose File | Open Directory on the main menu. Find the folder that contains your project source code, select it and open.

See also:
Running Tasks of Manage.Py Utility

Creating models

Let's begin with defining models. Open for editing the file of your application (select the file in the Project tool window, and press F4F4). By default, it only contains the import statement. We'll enter the model classes:

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

While you type, you have certainly paid attention to the syntax and error highlighting. Besides that, use the code completion ( Ctrl+Space⌃Space) on actually every stage of coding — PyCharm will smartly suggest the names of classes, methods, or fields, depending on the context:

Code Completion. Screenshot
Code Completion. Screenshot
Code Completion. Screenshot

Then, we need to create tables for the new model. For this purpose, we'll use the magic Ctrl+Alt+R⌥R shortcut twice:

  • First, select sql from the suggestion list, and choose the desired application name. This command generates SQL statements for both classes of the polls application:

    SQL. Screenshot
  • Second, select syncdb from the suggestion list to create tables, and see the following results in the console:

    syncdb. Screenshot
See also:
Auto-Completing Code

Performing administration functions

Since we've decided to enable site administration, PyCharm has already uncommented the corresponding lines in the file. However, we need to enable editing functionality for the admin site. To do that, create file in the polls directory ( Alt+Ins⌃N), and enter the following code:

from MyDjangoApp.polls.models import Poll, Choice
from django.contrib import admin

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'],'classes': ['collapse']})
    inlines = [ChoiceInline], PollAdmin)
Run configuration. Screenshot

Next, we are ready to go to the admin page and create some polls. Sure, it is quite possible to run the Django server, then go to your browser, and type the whole URL in the address bar, but with PyCharm there is an easier way: use the pre-configured Django server run configuration.

It is recommended to give it a name (in our case this is myapp), enable running the application in your default browser (select the Run browser check box), and specify the page of our site to be opened by default (

Run configuration. Screenshot

Now, to launch the application, press Shift+F10⇧F10, or click Run Icon on the main toolbar to open the standard Django's site administration page, where you have to log in. Then you can create polls and fill them out with questions and votes.

Django's site administration page. Screenshot

Creating views

We are now going to create views for our application: let it have the pages "archive", "details", "results", and the "votes" action. First of all, we have to add patterns for the new pages to the file. Open this file for editing (select this file in the Project tool window and press F4F4), and add patterns:

from django.conf.urls.defaults import patterns, include

from django.contrib import admin
urlpatterns = patterns('',
    (r'^polls/$', 'MyDjangoApp.polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'MyDjangoApp.polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'MyDjangoApp.polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', ''),
    (r'^admin/', include(,

These patterns refer to the views, which do not yet exist. You can spend some effort to create the view methods and associated templates manually, but it is much easier to use PyCharm's assistance: as you hover your mouse pointer over an unresolved reference, a yellow light bulb appears, which means that a quick fix is suggested. To show this quick fix, click the bulb, or, with the caret at the view name, just press Alt+Enter⌥↩.

Create view. Screenshot

Clicking the Create Django view method option results in creating a view method in the file, and the corresponding template file:

Create view. Screenshot

Besides the view methods, PyCharm generates the import statement that enables you to use render_to_response. Note also the icon in the left gutter next to the name of each view method — you can use it to navigate from the view method to its template. Having created all the required views methods and templates by means of the quick fix, let's fill them with the suitable code.

Open for editing the index.html file, and start typing the template code. First thing you will notice immediately, is the automatic pair braces completion: when you type {%, PyCharm adds the matching closing characters, and the caret rests in the next typing position. Here you can press Ctrl+Space⌃Space to show the list of suggested keywords:

Create template. Screenshot

The next invocation of code completion gives the available variables:

Create template. Screenshot

When it comes to typing HTML tags, PyCharm is ready to help you again:

  • Ctrl+Space⌃Space code completion shows the list of available tags.
  • When you type the opening angle bracket, the matching closing tag is generated automatically.

So, you fill your template step by step, and finally get something like the following examples (index.html and detail.html files), with syntax highlighting:

Create template. Screenshot
See also:
Navigating Between Templates and Views

Here we are!

Let's check the list of available polls and the details for a particular poll. Our admin site is already running, and the easiest way to visit the page that contains the list of polls (the index page), is to specify its URL in the address bar of the browser — instead of /admin/, type /polls/:

Result. Screenshot

Click any poll to view its details:

Result. Screenshot

This brief tutorial is over. You have successfully created and launched a simple Django application — congrats!