PyCharm 2017.2 Help

Part 6. Django-Specific Navigation

This feature is supported in the Professional edition only.

In this section:

Introduction

Finally, we'll explore the Django-specific navigation.

Note that same type of navigation is available for the other frameworks: Pyramid, Flask etc.

Use the example from the "first steps" guide Step 4. Creating and Running Your First Django Project.

Jumping from a view to a template

Open the file views.py for editing (F4). In the left gutter, next to the line template_name = "polls/index.html", you see the icon /help/img/idea/2017.2/classTypeHtml.png.

Hovering the mouse pointer over this icon reveals the following pop-up window:

/help/img/idea/2017.2/py_goto_template.png

Clicking this icon results in jumping directly to the template index.html, that resides in the folder polls under templates:

/help/img/idea/2017.2/py_template_index.png

Jumping from a template to a view

In the left gutter of the template file index.html, you see the icon /help/img/idea/2017.2/pythonScript.png. If you hover your mouse pointer over this icon, you'll see the following pop-up window:

/help/img/idea/2017.2/py_referenced_from_view.png

Clicking this icon leads you directly to the corresponding view.

Jumping between views and urls.py file

PyCharm allows you to easily navigate between a particular view and the corresponding url. This is how it's done:

  1. Hover your mouse pointer over the view name, while keeping Ctrl key pressed, and see the view name turning into a hyperlink:
    /help/img/idea/2017.2/py_jump_from_view_to_url.png
  2. If you click this hyperlink, you'll jump directly to the corresponding URL:
    /help/img/idea/2017.2/py_jump_from_view_to_url_result.png

Vice versa, you can also jump from a URL to the corresponding view. Again, in the urls.py file, hover your mouse pointer over the view name, while keeping Ctrl key pressed, and see the view name turning into a hyperlink.

Ctrl+click the view name - and find yourself in the corresponding view.

By the way, if you click a pin icon /help/img/idea/2017.2/pin.png in the pop-up, this pop-up becomes "pinned" - that is, turns into a tool window:

/help/img/idea/2017.2/py_jump_from_url_to_view_pinned.png

Finally, open a template in the editor (F4), say, index.html and hover your mouse pointer over detail, while keeping Ctrl key pressed, and see the name turning into a hyperlink. Click this hyperlink - you immediately jump to the file urls.py:

/help/img/idea/2017.2/py_jumping_from_template_to_url.png

Navigating to implementation and declaration

In a Django project, you see the same icons /help/img/idea/2017.2/gutterIconOverridden.gif and /help/img/idea/2017.2/gutterIconOverriding.png as in a pure Python project. When you hover your mouse pointer over such an icon, you see a pop-up window, like the following:

/help/img/idea/2017.2/py_django_navigation_with_gutter_icons.png

You can also jump to a declaration or implementation of a symbol. To do that, just place the caret at the implementing/overriding symbol and press Ctrl+B or Ctrl+Alt+B (or choose Navigate | Declaration or Navigate | Implementation on the main menu):

/help/img/idea/2017.2/py_django_goto_declaration_implementation.png

Jumping to tests

Django applications can be tested same as the pure Python ones. Same way you can create tests for the Django projects, for example, press Ctrl+Shift+T:

/help/img/idea/2017.2/py_django_create_test.png

Fill in the form in the Create test dialog:

/help/img/idea/2017.2/py_django_create_test_dialog.png

You'll see the code like:

from unittest import TestCase class TestQuestion(TestCase): def test_was_published_recently(self): self.fails()

The test class naturally fails, but it's up to you to write some meaningful code.

When a test already exists, you can jump to it, using the same keyboard shortcut Ctrl+Shift+T:

/help/img/idea/2017.2/py_django_jump_to_test.png

(Note that choosing a particular test runner can be done in the Settings/Preferences dialog, on the page Python Integrated Tools.)

Summary

This brief tutorial is over. You've mastered the following features:

  • Used the gutter icons to navigate between views and templates.
  • Navigated between views and urls.
  • Repeated how to jump to an implementation or declaration.
  • Repeated how to navigate to an existing test or create a new one.

See Also

Last modified: 9 August 2017