Most complete Pycharm tutorials (8) creation and management of--django engineering

Source: Internet
Author: User
Tags django server

Most complete Pycharm Tutorials (1)--Custom Skins

Most full Pycharm Tutorials (2)-code style

Most full Pycharm Tutorials (3)-code debugging, running

Most complete Pycharm Tutorials (4)--related configuration for Python interpreter

Most full Pycharm Tutorials (5)--python shortcut key Related settings

Most full Pycharm Tutorials (6)--using Pycharm as a VIM editor

1. Theme

This section of the tutorial focuses on creating, managing, and running a Django project through Pycharm. You can refer to the Python community for knowledge of the Django module.

2. Preparatory work

(1) Pycharm is 3.0 or higher.

(2) At least one Python interpreter is installed on the computer, and 2.4 to 3.3 versions are available.

The environment configuration used in this tutorial is as follows:

(1) The Django module version is 1.6.5

(2) default configuration for shortcut keys in Windows mode

(3) This part of the example is the same as the instance used in the description document of the Django module Django documentation

3. Create a new project

Virtually all project creation can be done by clicking the Create New Project button on the Welcome screen interface.

If you have already opened a project, you can file→new project by using the menu bar ... To create a new project. Next, enter the project name, the type of selection, and the version of the interpreter used in the Create New Project Dialog dialog box:

Click OK and the project's personalization is complete.

This means that the corresponding directory has been created and a. Idea directory is used to save the configuration information for project settings.

For an empty project, empty project, the creation of the link has been completed. Then you can start writing the program. But there is some work to be done on some of the supported third-party frameworks. Depending on the type of project selected, Pycharm will prompt us for some additional framework settings.

In this example, let's create a Django application to develop.

4. Create a Django Project

Therefore, in the Create New Project dialog box, our project type is selected as Django, note that Pycharm prompts us to install the Django framework if it is not available in the current environment.

Next we're going to set up the Django project:

Click OK to complete the setup.

5. Engineering Catalogue Structure

As mentioned above, the root structure of the project has been created, mainly including the basic framework configuration files and directories, when you create other types of projects, there will be similar operations, such as T Pyramid, or Google App Engine.

Next we look at how to display the engineering structure in the Project window.

6. Directory structure in the Project window

This is the default display mode. The structure shown in the window has polls and Mydjangoapp directories, and of course two Python files: manage.py and settings.py.

In this window you are unable to see the. IDEA directory structure.

7. Project files under Project window

If you want to see the idea directory, just select View Project files mode, and this view shows the same file as before, just the idea directory:

Ok, back to the previous view mode.

8. What are the files displayed in the Project window?

(1) Theuntitled directory is a project container, displayed in bold font in the window.

(2) manage.py is a command-line file that helps you manipulate your Django project, see product documentation

(3) Nested subdirectory Mydjangoapp acts as a library for the current project

(4) mydjangoapp/_init_.py is an empty file to indicate that the current directory should be used as a library.

(5) mydjangoapp/settings.py contains the relevant settings for the current project configuration for your Django project

(6) mydjangoapp/urls.py contains URL information for the current project response URL declarations for your Django project

(7) mydjangoapp/wsgi.py defines the portal of the Web server in Wsgi compatibility mode, as described in how to deploy with WSGI

(8) Thepolls directory contains all the files that complete the Django application (empty at this time):

Polls/_init_.py indicates that the current directory should be used as a library

polls/models.py Save the application model that we created

polls/views.py Preservation of our views

(9)templates is empty, template file to contain the response

It is worth mentioning that you can create many Django apps by running the manage.py file's startapp task to add it to the current project (the Tools→run manage.py Task) command on the main menu.

9. Configuration database

After the root is generated, we need to do some fine tuning. Open the file settings.py (select and press F4).

First determine what kind of database you are ready to use in your application. You can locate the databases variable by pressing CTRL+F, then entering the string you want to find in the search bar, and then entering the database management system (which is set to sqlite3) behind the colon of the ENGINE line.

On the ' name ' line, enter the name of the predefined database (whether or not it already exists):


10. Loading Django Services

Since we are here in a cautious choice of sqlite3 database. So there is no need to define other variables (such as user certificates, port numbers, post files, etc.). Next we check if our setup is correct, it is quite simple, as to load and run the manage.py file: Press CTRL+ALT+R, enter the task name in the message box that pops up:

11. Create a model

Next, open and edit the (open for editing) models.py file, note that at this point the Pycharm has been implemented to import the relevant library, and then type the following code:

In fact, directly to the above code to copy and paste, but it is recommended that you manually input to realize pycharm powerful spelling hint function:

12. Creating a Database

Next we need to add a form for the new model. To use the Ctrl+alt+r shortcut key again:

First select SQL from the list of prompts and select the desired app name:

This command automatically adds the SQL declaration for the current class:

In the second step, select the SYNCDB statement in the prompt list, create the list in the prompt list, and display the results as follows:

13. Perfect Management control function

Since we need account management of the application, Pycharm has already defined the relevant commands in the urls.py file.

However, we need to edit the function's admin function. Create a admin.py file (alt+ins) under the polls folder and enter the code:

Show pycharm powerful spelling hints again:

14. Operation and Commissioning

Now we're going to go to the admin interface and make some settings. Of course, we'll probably need to run the Django service first, enter the corresponding file directory, and enter the full URL address in the Address bar. But here Pycharm provides a lightweight way to modify it: The Django server Run configuration

Click the run/debug configurations option on the main toolbar to enter Debug configuration mode and select Edit Config (or select Run→edit configurations from the main menu):

In the Run/dug Config dialog Box dialog box, enter the configuration scheme name (MyApp here), the default browser (tick the Run browser option), and personalize our node interface:

15. Loading user interface

Load and run the app, press SHIFT+F10 or the Run button on the main toolbar to open the standard administration page, and you must log in. Next you can create some polls and develop appropriate questions and candidates for them:

16. Create a View link

Next we are going to add some sub-views to the app that have "index", "Details", "results", "votes" and other sub-pages. First, we add the pattern of these sub-pages to the urls.py file (select the file in the Project window and press F4):

The pages involved in these patterns do not currently exist, so there is a need to manually add some methods to them and make template associations, which can be surprisingly simple with the help of pycharm: you simply hover the mouse pointer over an undefined (Pycharm will highlight those unresolved Reference code), which will light up a small yellow light bulb, which means pycharm here to prepare a quick tip, click on the small bulb (or press Alt+enter):

Select the Create Django View method option to create a view's member method in the views.py file and associate it with a specific template file.

Next we will see the following changes:

  The templates directory is no longer empty and contains the root template files we created.

The views.py file already contains the relevant methods for the root view.

In addition to adding a view-related method, Pycharm automatically imports the relevant actions in Django and marks them with Render_to_response.

Note the icon to the left of the view method name, which you can use to view the template for that method. You can quickly create a view and the corresponding template through the Create Template<name> command, and then we write the code to it.

For example, we want to see the available list of polls, open views.py, and enter the following code:

Pycharm will give you a quick spelling hint:

The following will be shown when finished:

17. Create a template

Next we add some code to the template. Open the Index.html file and enter the template code. It is important to note that curly braces must appear in pairs when you enter {%,pycharm) to automatically add another parenthesis after the input cursor. Here you can use Ctrl+space to spell tips.

When you need to enter HTML type tags, pycharm also designed a Help system:

Ctrl+space calls the spelling hint feature.

When you enter a parenthesis, another bracket is automatically generated to match

Next step-by-step evening Your template code, the final result is as follows:

18. Accomplished

Let's check the list of polls variables, find out that our admin is working properly, and be able to display the corresponding URL address in the Address bar (/admin/, type/polls/):

Click to view more information:

Most complete Pycharm tutorials (8) creation and management of--django engineering

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.