In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the method of the creation and management of the Django project". In the daily operation, I believe that many people have doubts about the method of the creation and management of the Django project. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of the creation and management of the Django project?" Next, please follow the editor to study!
1. Theme
This tutorial focuses on how to create, manage, and run a Django project through Pycharm. For the relevant knowledge of Django module, you can refer to the Python community.
2. Preparatory work
(1) Pycharm is 3.0 or higher.
(2) at least one Python interpreter is installed on the computer, and versions 2.4 to 3.3 are available.
The environment used in this part of the tutorial is configured as follows:
(1) the version of Django module is 1.6.5
(2) the default is shortcut key configuration in Windows mode.
(3) these examples are the same as the examples used in the documentation of the Django module. Django documentation
3. Create a new project
Virtually all projects can be created by clicking the Create New Project button on the Welcome screen interface.
If you have already opened a project, you can use the menu bar File → New Project... To create a new project. Next, enter the project name, select the type, and the interpreter version used in the Create New Project dialog dialog box:
Click OK, and the personalization of the project is complete.
This means that the corresponding directory has been created, and a .idea directory is pre-defined to hold the configuration information project settings.
For an empty project empty project, the creation process is complete. Then you can start writing programs. But for some of the supported third-party frameworks, there is still some work to be done. Depending on the type of project you choose, Pycharm prompts us to make some additional framing.
In this example, let's create and develop a Django application.
4. Create a Django project
Therefore, in the Create New Project dialog box, our project type is selected as Django, and notice that Pycharm will prompt us to install the Django framework if it is not available in the current environment.
Next, let's set up the Django project:
Click OK to complete the setup.
5. Project catalogue structure
As mentioned above, the root directory structure of the project has been created, which mainly contains basic framework configuration files and directories, and there will be similar actions when you create other types of projects, such as t Pyramid, or Google App Engine.
Next we'll look at how to display the engineering structure in the Project window.
6. Directory structure in Project window
This is the default display mode. The structures that will be displayed in the window are the polls and MyDjangoApp directories, as well as two Python files: manage.py and settings.py.
You can't see the .idea directory structure in this window.
7. The project files under the Project window
If you want to see the idea directory, just select view Project Files mode, and this view shows the same files as before, except that there are more idea directories:
Ok, go back to the previous view mode.
8. What are the files displayed in the Project window for?
(1) the untitled directory is the container of the project and is displayed in bold font in the window.
(2) manage.py is a command line file that helps you operate your Django project. For more information, please see product documentation
(3) the nested subdirectory MyDjangoApp acts as the library for the current project.
(4) MyDjangoApp/init.py is an empty file that indicates that the current directory should be used as a library.
(5) MyDjangoApp/settings.py contains the relevant settings configuration for your Django project of the current project.
(6) MyDjangoApp/urls.py contains the url information of the current project response URL declarations for your Django project
(7) MyDjangoApp/wsgi.py defines the entry of Web server in WSGI compatibility mode. For more information, please see How to deploy with WSGI.
(8) the polls directory contains all the files for improving the Django application (empty at this time):
Polls/init.py indicates that the current directory should be used as a library
Polls/models.py saves the application model we created
Polls/views.py saves our views
(9) templates is empty and is used to contain the template file of the response
It is worth mentioning that you can create a lot of Django applications and add them to the current project (Tools → Run manage.py task on the main menu) by running the startapp task of the manage.py file.
9. Configure the database
After the root directory is generated, we need to make some fine-tuning. Open the file settings.py (press F4 when selected).
First determine what kind of database you are going to use in your application. You can locate the DATABASES variable by pressing Ctrl+F, entering the string you want to find in the search bar, and then entering the database management system used after the colon on the 'ENGINE' line (set to sqlite3 here).
In the 'NAME' line, enter the name of the predefined database (whether it already exists or not):
10. Load Django service
Because we chose the sqlite3 database carefully here. So there is no need to define other variables here (such as user certificates, port numbers, POST files, and so on). Next, let's verify that our settings are correct, which is quite simple. As for loading and running the manage.py file, press Ctrl+Alt+R and enter the task name in the pop-up message box:
11. Create a model
Next, open and edit the (open for editing) models.py file, notice that Pycharm has implemented and imported the related libraries, and then type the following code:
In fact, you can copy and paste the above code directly, but it is recommended that you enter it manually to experience the powerful spelling prompt function of Pycharm:
12. Create a database
Next we need to add a form for the new model. Use the Ctrl+Alt+R shortcut again:
First select sql from the list of prompts, and then select the expected application name:
This command automatically adds a SQL declaration to the current class:
In the second step, select the syncdb statement in the prompt list, and create the list in the prompt list. The result is as follows:
13. Improve the management control function
Since we need to manage the account of the application, Pycharm has defined the relevant commands in the urls.py file.
However, we need to edit the admin function of the function. Create an admin.py file (Alt+Ins) under the polls folder, and enter the following code:
Once again, show off the powerful spelling prompt function of Pycharm:
14. Operation and debugging
Now we are ready to go to the admin interface to make some settings. Of course, we 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: Django server run configuration
Click the run/debug configurations option on the main toolbar to enter debug configuration mode, and then select Edit Configuration (or select Run → Edit Configurations from the main menu):
In the Run/Dug Configuration dialog box dialog box, enter the name of the configuration scheme (myapp in this case) and the default browser (check the Run browser option) to customize our node interface:
15. Load user interface
To load and run the application, press the Shift+F10 or run button in the main toolbar to open the standard administration page, and you must log in. Next you can create some polls and make corresponding questions and candidates for them:
16. Create a view link
Next, we are going to add some child views to the application, so that it has "index", "details", "results", "votes" and other sub-pages. First, we add the mode of these subpages to the urls.py file (select the file in the Project window and press F4):
The pages involved in these modes do not yet exist, so you need to manually add some methods to them and do template association, which can be extremely simple with the help of Pycharm: all you have to do is hover the mouse pointer over an undefined (Pycharm will highlight the code of those unresolved reference), which will light up a small yellow light bulb, which means that Pycharm has prepared a quick prompt here. Click the small light bulb (or press Alt+enter):
Select the Create Django view method option to create a member method of a view 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 file we created.
Methods related to the root view are already included in the views.py file.
In addition to adding view-related methods, Pycharm also automatically imports related operations in Django and marks them with render_to_response.
Notice the icon to the left of the view method name, which you can use to view the template for this method. You can quickly create the view and the corresponding template through the Create template command, and then write code to it.
For example, if we want to see the available list of polls, open views.py and enter the following code:
Pycharm will give you quick spelling tips:
After completion, it will be displayed as follows:
Create a template
Next, let's 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 type {%, Pycharm will automatically add another parenthesis after the input cursor. Here you can use Ctrl+Space for spelling prompts.
When you need to enter a HTML type tag, PyCharm also designs a help system:
Ctrl+Space invokes the spelling prompt function.
When you enter a parenthesis, another parenthesis is automatically generated to match
Next, as for your template code step by step, the final result is as follows:
18. The job is done
Let's verify the variable list of polls and find that our admin works properly and can display the corresponding URL address (/ admin/, type / polls/) in the address bar:
Click to view details:
At this point, the study on "what is the method of creating and managing the Django project" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.