Web application of python application chapter - Django introduction (on)

foreword

  We give you a general introduction to the production process of data visualization through seven articles . Of course, this project is also from the "Python Programming From Introductory to Practice book project" edited by Eric Matthes. However, I will introduce this project to you through certain characteristics and the way I study the project. From the construction of the environment to the implementation step by step. Of course, with the deepening of the project, the amount of code is getting larger and larger. In order to facilitate everyone's reading, we only write the code method corresponding to the realization function. Today, we give you the complete code of the project and give you a complete effect. However, readers are strongly encouraged to read from the beginning of this project. In this way, I believe it will benefit you a lot. If you just paste all the code in this article, it doesn't work, maybe you don't even know the whole framework of this project.
  Today's websites are actually rich applications, just like full-fledged desktop applications. But to tell the truth, when it is generally used for software development, the language of choice is Java or C++. Currently, there is also a switch to the go language, but Python development and applications are still relatively less popular. Therefore, everyone regards this case as a Python project practice is enough, it is not very useful. In fact, I have been struggling for a long time, whether to write this project or not, but in order to let everyone have one more Python practice project, I finally decided to write it. But the mainstream is still java. In fact, I have also introduced to you before. Although the technology is figured out, any kind of computer industry can be implemented in a language, but each language has its own specific areas to deal with. For us, Python, more What is done is data analysis and data processing to do algorithms. So the former project actually represents a trend towards more Python.
  This article begins to introduce a very important knowledge point in Python - Django is a web framework, a set of tools used to help develop interactive websites. Django can respond to website requests, and it also makes it easier for you to read and write databases, manage users, and more.It should be noted here that this project is also from "Python programming from entry to practice". First of all, we will introduce the whole process of the project to you.

1. Formulate specifications

  When building a project, you first need to describe the project in a canonical way, and then set up a virtual environment in which to create the project.
  The complete specification details the goals of the project, articulates the functionality of the project, and discusses the appearance and user interface of the project. As with any good project plan and business plan, specifications should focus and help keep the project from going off track. There will not be a complete project plan, but only some clear goals to highlight the development focus. We develop normative items as follows:

  We're going to write a web application called "Study Notes" that will allow users to record topics of interest and add log entries as they study each topic. The home page of "Study Notes" describes the site and invites users to register or log in. Once logged in, users can create new topics, add new entries, and read existing entries.

  As we learn new topics, recording what we learn can help keep track and review it. A great app makes this recording process simple and easy.

2. Create a virtual environment

  To use Django, you first need to set up a virtual work environment. A virtual environment is a location on the system where we can install various packages and will be isolated from other Python packages. It is beneficial to separate the project's library from other projects, and is of course necessary in order to deploy the "study notes" to the server next.
  Create a new directory for the project. It should be noted here that the directory should preferably not have Chinese, but English. Our newly created directory is named learning_log, then switch to this directory in the terminal, and create a virtual environment. If the reader is also using Python3, you can use the following command to create a virtual environment:

learning_log$ Python -m venv ll_env

  The specific result is as follows:

  here we run the module venv and use it to create a ll_envvirtual environment called . If you are the same as me in the picture above, it means that you have installed virtualenvthis environment. If an error is reported, we have to install virtualenvit. Next, I will introduce virtualenvthe installation of the third-party library to you.

3. Installationvirtualenv

  If we are using an earlier version of Python, or if the system is not properly set up to use the module venv, install the virtualenv package. To do this, execute the following command:

$ pip install --user virtualenv

  For this command, we may need to use different commands for different versions. But what we need to pay attention to is: if we are using a Linux system and the above command is not satisfied, we can use the system's package manager to install virtualenv. For example, to install virtualenv in Ubuntu system, we can use the following command (But only for Ubuntu system, other commands are not applicable)

sudo apt-get install python-virtualenv

  Switch to the previous introduction in the terminal learning_logand create a virtual environment like this:

learning_log$ virtualenv ll_env

  What we need to pay attention to here is that if we have multiple Python versions installed on the system, we need to specify the version used by virtualenv. For example, the command virtualenv ll_env --python=python3creates a virtual environment using Python3.

Fourth, activate the virtual environment

  After creating the virtual environment, we need to activate it with the following command:

learning_log$ source ll_env/bin/activate

  This command runs ll_env/binthe script activate. When an environment is active, the environment name will be enclosed in parentheses, in which case we can install packages in the environment and use the installed packages. The packages we ll_envinstall in are only available when that environment is active. but,If we are using a windows system, we can directly use the command ll_env\Scripts\activate(without source) to activate this virtual environment. The specific operating environment is as follows:

  If the activation is successful, the specific implementation effect is as follows:

  To stop using the virtual environment, execute the command deactivate. The specific execution command is as follows:

(ll_env)learning_log$ deactivate

  If you close the terminal running the virtual environment, the virtual environment will also no longer be active. The specific operation diagram is as follows:

Five, install Django

  Once we have created and activated the virtual environment, we can install Django,For the stability of our project later, we use Django version 1.11. We install Django with the following command:

(ll_env)learning_log$ pip install Django==1.11

  The specific effect is as follows:

  Since we are working in a virtual environment, the commands for installing Django are the same on all systems; you do not need to specify the flag –user, and you also need to use the following commands:

python -m pip install package_name 

  The installation is successful if the following results appear, and the specific execution results are as follows:

  What needs to be reminded here is:Django only works when a virtual environment is active

6. Create a project in Django

  With the virtual environment still active (ll_env enclosed in parentheses), execute the following command to create a new project:

(ll_env)learning_log$ django-admin startproject learning_log .

  The specific commands are as follows:

  Then we can view the relevant content through the following commands:

  We first create a command to let Django create a new learning_logsproject named. The period at the end of this command makes the new project use the proper directory structure so that the application can be easily deployed to the server after development is complete. What needs to be reminded here is that when entering the above command, do not forget the last period. Otherwise, the application will encounter some configuration problems when deploying. If you forget the period, delete the created files and folders (except ll_env) and rerun the command. Then I ran the command dir, which showed that Django created a new learning_logdirectory named . It also creates a manage.pyfile called , which is a simple program that takes commands and hands them off to the relevant parts of Django to run. We will use these commands to manage tasks such as working with databases and running servers.
  The directory learning_logcontains 4 files, the most important of which are settings.py, urls.pyand wsgi.py. Files settings.pyspecify how Django interacts with our system and manages projects. As we develop our project, we will modify some of these settings, and add a few more. The file urls.pytells Django which web pages should be created in response to browser requests. file wsgi.pyhelps Django serve the created file, this filename is web server gateway interface(web服务器网关接口)的首字母缩写.
  However, there is a small knowledge point hidden here. If we accidentally enter the creation command as: django-admin.py startproject learning_log .if there is nothing after execution and no error is reported, then remove .py and try it. Because, after checking the information on the Internet, I found that:
  Create an APP, enter the project directory and execute: python manage.py startapp learning_log .. Therefore, when we enter the command, it is very important, and please pay special attention. And there are indeed two files under scripts, as follows:
database

Seven, create a database

  Django stores most project-related information in a database, so we need to create a Djangodatabase to use. To create a database for the project "Study Notes", execute the following command while in the active virtual environment:

(ll_env)learning_log$ python manage.py migrate

  We refer to modifying the database as migrating the database. When the command migrate is executed for the first time, it will let Django ensure that the database matches the current state of the project. The first time this command is executed in a new project using SQLite, Django will create a new database. In addition, Django pointed out that it will create the necessary database tables to store the information we will use in this project, and

  then make sure that the database structure matches the current code. dirCreated a file - db.sqlite3. SQLite is a single-file database ideal for writing simple applications because it lets you worry less about database management. The specific inquiries are as follows:

8. View the project

  Let's verify that Django created the project correctly. To do this, execute the command runserver, which is implemented as follows:

python manage.py runserver

  Django starts a server that allows us to look at the projects in the system and see how they work. When we enter a url in the browser to request a web page, this Django server will respond; generate the appropriate web page, and send it to the browser.
  In addition, Django checks to confirm that the project was created correctly; then it indicates the version of Django used and the name of the settings file currently in use; second, it indicates the URL of the project. The URL http://127.0.0.1:8000/ indicates that the project will be listening for requests on port number 8000 of your computer (ie localhost). localhost is a server that only handles requests from the current system, when no one else is allowed to view the web page we are developing.
  Finally we open a browser and enter the URL: http://localhost:8000/; if this doesn't work, enter http://127.0.0.1:8000/. We will see an interface similar to the one shown in the image below. This interface was created by Django to let us know that everything is working right now. Do not shut down this server just yet. If you want to shut down this server, press and hold ctrl+c, the specific situation is as follows:

  Here we need to remind everyone: if the error message "That port is already in use (here means that the specified port is already occupied)" appears, please Execute the command python manage.py runserver 8001to make Django use another port; if this port is also not available, keep executing the above command and gradually increase the port number until you find one that is available.

Summarize

  We give you a general introduction to the production process of data visualization through seven articles . Of course, this project is also from the "Python Programming From Introductory to Practice book project" edited by Eric Matthes. However, I will introduce this project to you through certain characteristics and the way I study the project. From the construction of the environment to the implementation step by step. Of course, with the deepening of the project, the amount of code is getting larger and larger. In order to facilitate everyone's reading, we only write the code method corresponding to the realization function. Today, we give you the complete code of the project and give you a complete effect. However, readers are strongly encouraged to read from the beginning of this project. In this way, I believe it will benefit you a lot. If you just paste all the code in this article, it doesn't work, maybe you don't even know the whole framework of this project.
  This article begins to introduce a very important knowledge point in Python - Django is a web framework, a set of tools used to help develop interactive websites. Django can respond to website requests, and it also makes it easier for you to read and write databases, manage users, and more.It should be noted here that this project is also from "Python programming from entry to practice". This article mainly introduces you to the establishment of the project, including formulating specifications, establishing a virtual environment, installing virtualenv, activating the virtual environment, installing Django, creating a project in Django, creating a database, and finally viewing the project. Python is a practical language, it is the simplest of many programming languages, and it is also the best to get started. When you have learned the language, it is relatively simple to learn java, go and C. Of course, Python is also a popular language, which is very helpful for the realization of artificial intelligence. Therefore, it is worth your time to learn. Life is endless and struggle is endless. We work hard every day, study hard, and constantly improve our ability. I believe that we will learn something. come on! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324288920&siteId=291194637