Turn: Pycharm Community Debug Django Projects

Source: Internet
Author: User
Tags version control system virtual environment django tutorial pycharm community edition

Original: https://automationpanda.com/2017/09/14/django-projects-in-pycharm-community-edition/comment-page-1/

Note the section "Creating Run configurations", which describes how to use the Pycharm Community Debug Django Project

Django Projects in Pycharm Community Edition

JetBrains Pycharm is one of the best Python IDEs around. It ' s smooth and intuitive–a big step up from Atom or notepad++ for big projects. Pycharm is available as a standalone IDE or as a plugin for it big sister, IntelliJ idea. The free Community Edition provides basic features akin to IntelliJ, while the licensed Professional Edition provides Adva Nced features such as Web development and database Tools. The Professional Edition isn ' t cheap, though:a license for one user could cost up to $199 annually (though discounts and FR EE licenses may available).

This guide shows how to develop Django Web projects using Pycharm Community Edition. Even though Django-specific features be available only in Pycharm Professional Edition, it's still possible to develop D Jango projects using the "free version" with help from the command line. Personally, I ' ve been using the free version of Pycharm to develop a small web site for a side business of mine. This guide covers setup steps, basic actions, and feature limitations based on my own experiences. Due to the limitations in the "free version", I recommend it only for small Django projects or for hobbyists who WA NT to play around.

Prerequisites

This guide focuses specifically the configuring Pycharm Community Edition for Django development. As such, readers should is familiar with Python and the Django web framework. Readers should also is comfortable with the command line for a few actions, specifically for Django admin commands. Experience with JetBrains software like Pycharm and IntelliJ idea is helpful but not required.

Python and Pycharm Community Edition must is installed on the development machine. If you aren't sure which version of Python to use, I strongly recommend Python 3. Any required Python packages (namely Django) should is installed via PIP.

Creating Django Projects and Apps

Django projects and apps require a specific directory layout with some required settings. It's possible to create this content manually through Pycharm, but it's recommended to use the standard Django commands Instead, as shown in Part 1 of the official Django tutorial.

> Django-admin startproject newproject> cd newproject> django-admin Startapp Newapp

Then, open the new project in Pycharm. The files and directories is being visible in the Project Explorer view.

The project root directory should is at the top of the project Explorer. The. Idea folder contains ide-specific config files, is not relevant for Django.

Creating New Files and directories

Creating new files and directories are easy. Simply Right-click the parent directory in Project Explorer and select the appropriate file type under New. Files May is deleted using right-click options as well or by highlighting the file and typing the Delete or Backspace key.

Files and folders is easy to visually create, copy, move, rename, and delete.

Django projects require a specific directory structure. Make sure to put files in the right places with the correct names. Pycharm Community Edition won ' t check for you.

Writing New Code

Double-click any file in Project Explorer to open it in an editor. The Python Editor offers all standard IDE features like source highlighting, real-time error checking, code completion, an D Code Navigation. This is the main reason why I use pycharm over a simpler editor for Python development. Pycharm also have many keyboard shortcuts to make actions easier.

Nice.

Editors for other file types, such as HTML, CSS, or JavaScript, could require additional plugins not included with Pycharm C Ommunity Edition. For example, Django templates must are edited in the regular HTML editor because the special editor are available only in th E Professional Edition.

Workable, but isn't as nice.

Running Commands from the Command line

Django Admin commands can run from the command line. Pycharm automatically refreshes any file changes almost immediately. Typically, I switch to the command line to add new apps, make migrations, and update translations. I also created a few aliases for easier file searching.

> Python manage.py makemigrations> python manage.py migrate> python manage.py makemessages-l zh> python manag e.py compilemessages> python manage.py test> python manage.py collectstatic> python manage.py runserver
Creating Run Configurations

Pycharm Community Edition does not include the Django manage.py utility feature. Nevertheless, it's possible to create run configurations for any Django admin command so this they can be Run in the IDE Instead of the command line.

First, make sure, the Project SDK is set. From the File menu, select Project Structure.... Verify a project SDK is assigned on the Project tab. If not and then need to create a new one–the SDK should be the Python installation directory or a virtual Environme NT. Make sure to save the new Project SDK setting by clicking the OK button.

Don ' t leave that Project SDK blank!

Then from the Run menu, select Edit configurations.... Click the plus button in the Upper-left corner to add a Python configuration. Give the config a good name (like "Django: <command>"). Then, set script to ' manage.py ' and Script parameters to the name and options for the desired Django ADM Command (like "Runserver"). Set working directory to the absolute path of the project root directory. Make sure the appropriate Python SDK was selected and the PYTHONPATH settings is checked. Click the OK button to save the CONFIG. The command can then is run from Run menu options or from the run buttons in the Upper-right corner of the IDE WI Ndow.

Run configurations should look like this. Anything done on the command line can also is done here.

When commands is run, the run view appears at the bottom of the IDE window to show console output.

Special run configurations is particularly useful for the "test" and "runserver" commands because they enable rudimentary Debugging. You can set breakpoints, run the command with debugging, and step through the Python code. If you need to interact with a Web page to exercise the code, Pycharm'll take-screen focus once a breakpoint are hit. Even though debugging Django templates is isn't possible in the "free version", debugging the Python code can help identify MO St problems. Be warned this debugging is typically a bit slower than normal execution.

Debugging makes Django development so much easier.

I typically use the command line instead of run configurations for other Django commands just for simplicity.

Version Control Systems

Pycharm have Out-of-the-box support for version control systems like Git and Subversion. VCs actions is available under the VCs menu or when right-clicking a file in Project Explorer. Pycharm can directly check out projects from a repository, add new projects to a repository, or automatically identify the Version control system being used when opening a project. Any VCS commands entered at the command line is automatically reflected in Pycharm.

Pycharm ' s VCS menu is initially generic. Once you select a VCs for your project, the options would be changed to reflect the chosen VCs. For example, Git would has options for "Fetch", "pull", and "Push".

Personally, I use Git with either GitHub or Atlassian bitbucket. I prefer to does most Git actions like graphically through pycharm, but occasionally I drop to the command line when I need To does more advanced operations (like checking commit IDs or forcing hard resets). Pycharm also has support for. gitignore files.

Python Virtual Environments

Creating Virtual Environments is a great the to manage Python project dependencies. Virtual environments is especially useful when deploying Django Web apps. I strongly recommend setting up a virtual environment for every Python project you develop.

Pycharm can use virtual environments to run the project. If a virtual environment already exists, then it can be set as the project SDK under project Structure as Describ Ed above. Select New ... > Python SDK > Add Local, and set the path. Otherwise, new virtual environments can be created directly through Pycharm. Follow the same process to add a virtual environment, but under Python SDK, select Create virtualenv ins Tead of Add Local. Give the new virtual environment an appropriate name and path. Typically, I put my virtual environments either all on one common place or one level up from my project root directory.

Creating a new virtual environment is pretty painless.

Databases

Out of the box, Pycharm Community Edition won ' t give you database Tools. You ' re stuck with Third-party plugins, the command line, or external database tools. This isn ' t terrible, though. Since Django Abstracts data into the Model layer, most developers rarely need to directly interact with the underlying DAT Abase. Nevertheless, the Open-source Database Navigator plugin provides support on Pycharm for the major databases (Oracle, MySQL , SQLite, PostgreSQL).

Limitations

The sections above show that Pycharm Community Edition can handle Django projects just like any other Python projects. This was a blessing and a curse, because advanced features was available only in the Professional Edition:

    • Django Template Support
    • Inter-project navigation (view to template)
    • Better Code Completion
    • Identifier resolution (especially class-to-instance fields)
    • Model Dependency Graphs
    • manage.py Utility Console
    • Database Tools

The "the" and the "matter" most to me is the template support and the better code completion. With templates, I sometimes make typos or forget closing tags. With code completion, not all options is available because Django does some interesting things with model fields and Dyna mically-added attributes. However, all these missing features is "Nice-to-have" but not "need-to-have" for me.

Conclusion

I hope you found this guide useful! Feel free-enter suggestions for better usage, the comments section below. Also want to look at alternative IDEs, such as PyDev.

Turn: Pycharm Community Debug Django Projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.