[toc]


Code every python article well.


Everybody is good! I haven’t updated my article for a while. Yes, I am really busy. If you want to write an article, it really takes a few days, because you want to ensure the quality of the article.

Behind the article I will write some basic theory and actual combat article, after all, dry technology, just look at the theory will not be so much patience, in short, I will interlude some, to ensure that you are satisfied with the officer.

1. Environment preparation

The serial number The IP address The operating system Install the software note
1 192.168.8.130 CentOS 7.6.1810 Python3.6 + django2.2 All run in a virtual environment
2 192.168.8.131 CentOS 7.6.1810 mysql Mysql data
3 192.168.8.1 windows 10 PyCharm Pro Professional edition can use remote synchronization configuration with centos
  • For the CentOS7 installation environment, please search for the installation documents in Google or Baidu. This step is omitted.

  • Python Environment Installation

    Pyenv virtual environment management Python multiple versions and software libraries refer to this article.

    How to update PIP source steps:

    [root@localhost ~]# mkdir .pip [root@localhost ~]# cd .pip/ [root@localhost .pip]# touch pip.conf [root@localhost .pip]#  vim pip.conf [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com # After adding or modifying, remember to save.Copy the code

How to update the YUM source steps:

# install wget. Skip [root@localhost ~]# yum install wget -y # backup yum source [root@localhost python]# CD /etc/yom.repos. D / [root@localhost Repos. D]# mkdir repo_bak [root@localhost yum. Repos. D]# mv *.repo repo_bak/ # download new repo [root@localhost yum Yum cache wget http://mirrors.aliyun.com/repo/Centos-7.repo # removal systems and generate new yum cache [root @ localhost. Yum repos. D] # yum clean all [root@localhost yum. Repos. D]# yum makecache # Install EPEL (Extra Packages for Enterprise Linux) source [root@localhost [root@localhost yum. Repos. D]# yum clean all [root@localhost yum [root@localhost yum. Repos. D]# yum makecache # check the system available yum sources and all yum sources [root@localhost yumCopy the code

PIP source update reference link

YUM source update reference link

2. Start the installation

2.1 install Django

This CentOS IP address is 192.168.8.130 (through NAT mapping, can access the Internet)

# Install Django2.2 (py369) [python@localhost Python]$PIP install Django ==2.2 # Check the installed version information (py369) [python@localhost Python]$Python -m Django --version 2.2 # install mysqlClient [python@localhost Python]$PIP install mysqlClientCopy the code

2.2 Installing the Mysql Database

CentOS IP address 192.168.8.131 (NAT mapping allows access to Internet)

  • Installation and Startup

    Download the following installation packages in advance and upload them to the root directory using winscp:

    Repo.mysql.com/yum/mysql-5…

    Copy the link above and download it through your browser for faster speeds.

[root @ # installed mysql and expand localhost ~] # wget - I - c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install [root@localhost ~]# systemctl start mysqld.service # mysql-community-server-5.7.31-1.el7.x86_64 [root@localhost ~]# systemctl status mysqld.service... Active: active (running) since Tue 2020-07-21 10:14:23 CST; 2s ago ... Omit character hereCopy the code
  • Change passwords and create databases
# uQjeSi? [root@localhost ~]# grep "password" /var/log/mysqld.log 2020-07-21t20:48:28.965616z 1 [Note] A temporary password password is generated for root@localhost: uQjeSi? [root@localhost ~]# mysql -uroot -p Enter password:uQjeSi? Mysql > ALTER USER 'root'@'localhost' IDENTIFIED BY 'DevOps@2020'; Mysql > grant all on *.* to 'root'@'%' identified by 'DevOps@2020'; Query OK, 0 rows affected, 1 warning (0.00 SEC) # mysql> flush PRIVILEGES; Query OK, 0 rows affected (0.00 SEC) # mysql> CREATE DATABASE IF NOT EXISTS devops default charset utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 SEC) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | devops | | mysql | | Performance_schema | | sys | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 5 rows in the set (0.00 SEC) # withdrew from the database, modify the configuration file, the result is as follows:  [root@localhost ~]# cat /etc/my.cnf |grep -v ^#|grep -v ^$ [client] default-character-set = utf8 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log Pid - file = / var/run/mysqld/mysqld. Pid character - set - server = utf8 collation - server = # utf8_general_ci restart the database [root@localhost ~]# service mysqld restartCopy the code
  • Database connection verification is normal:

    Mysql > install mysql;

C:\>pip install mysqlclientCopy the code

Note: I imported pymysql through Pycharm for the test.

3. Start the configuration

You should have a preliminary understanding of this kind of graph.

  • When a diaosi opens a browser to visit a page, type www.baidu.com and press Enter;

  • Django background urls will match links. Urls can be considered HTTP links, which are technically called routes.

    Description: Not the routing protocol we understand.

  • When the urls match, they look up the code in the View View and respond;

    View is the Python code we will write

  • Template is simply an HTML page template;

  • Model simply understands a table in a database;

3.1 Django Simple Configuration

3.1.1 Creating a DevOPS Project

# create a new project (py369) [python@localhost Python]$ Django -admin StartProject DevOps (py369) [python@localhost Python]$tree DevOps DevOps ├─ DevOps │ ├─ __init__ ├ ─ ─ __pycache__ │ │ ├ ─ ─ just set retaining - 36. Pyc │ │ ├ ─ ─ Settings. Retaining - 36. Pyc │ │ ├ ─ ─ urls. Retaining - 36. Pyc │ │ └ ─ ─ Wsgi.cpython-36. Pyc │ ├── settings.py │ ├─ urlsCopy the code

3.1.2 Remotely synchronize the server code by PyCharm

Pycharm downloads django projects to Windows for editing, and then synchronizes them to CentOS in real time.

1) Open PyCharm and click Tools->Deployment->Configuration

2) Create a new service name as follows:

3) Download the remote Django project:

4) Modify the automatic synchronization configuration, Tools->Deployment->Options

3.1.3 Modifying Django configuration Files directly on PyCharm

1) the conversation – > the conversation – > Settings. The py:

Database = {'default': {'ENGINE': 'django. Db. Backends. Mysql', 'NAME' : 'enterprise', 'HOST' : '192.168.8.131', 'USER' : 'root' and 'PASSWORD' : 'root @ 123', 'the PORT: 3306, LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'Copy the code

2) After the configuration changes are made, they are automatically synchronized to remote Django. Extract the log part:

2020/3/27 23:07] Automatic upload completed in 33 ms: 1 file transferred (103.6 kbit/s)

3.1.4 Starting the Django Service

1)通过命令启动:python manage.py runserver 0.0.0.0:8888

Py369 [python@localhost devops]$python manage.py runServer 0.0.0.0:8888 Watching for file changes with StatReloader Performing system checks... System Check identified no issues (0 silenced). March 27, 2020-23:45:14 Django Version 2.2, Using Settings 'devops. Settings' Starting development server at http://0.0.0.0:8888/ Quit the server with control-c.Copy the code

2) open the browser, enter http://192.168.8.130:8888:

3) can also enter the background rights management system, enter http://192.168.8.130:8888/admin

In this case, the super administrator user and password have not been created and cannot be logged in.

4) Next, start your own APP admin

(py369) [python@localhost devops]$ python manage.py migrateCopy the code

Note: This command will write the admin app database of the system into mysql.

5) Log in to the mariadb database and check whether the data is written properly

[root@localhost ~]# mysql -u root -pDevOps@2020 # mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ddjsdb | | devops | | devops01 | | Mysql | | performance_schema | | sys | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 7 rows in the set # (0.73 SEC) to switch to the database mysql > use the conversation in the conversation Mysql > show tables; +----------------------------+ | Tables_in_devops | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 10 Rows in set (0.00 SEC)Copy the code

6) Create an administrator user

Py369 [python@localhost devops]$python manage.py createcacheTableCopy the code

7) Successfully log in to background management system

# Start Django (py369) [python@localhost devops]$python manage.py runServer 0.0.0.0:8888Copy the code

3.2 Creating a new APP

3.2.1 Creating a New App Hello

(py369) [python@localhost devops]$python manage.py startApp Hello # Manually create urls.py file (py369) [python@localhost devops]$ [python@localhost devops]$Tree Hello Flag school ─ admin.py Flag school ─ apps.py Flag school ─ Just set py ├ ─ ─ migrations │ └ ─ ─ just set py ├ ─ ─ models. Py ├ ─ ─ tests. Py ├ ─ ─ urls. Py └ ─ ─ views. PyCopy the code

3.2.2 PyCharm Synchronous Download of the Remote Hello Directory

Note: The method is the same as section 3.1.2.

3.2.3 Modifying Django Configuration Scripts

1) Modify the devops->devops->setting.py configuration

INSTALLED_APPS = [hello.apps.HelloConfig]Copy the code

2) Modify the devops->hello->views.py configuration

from django.shortcuts import render
from django.http import HttpResponse


def index(request):
    return HttpResponse('<p>hello django</p>')Copy the code

3) Modify the devops->hello->urls.py configuration

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.index, name='index'),
]Copy the code

4) Finally modify the devops->devops->urls.py configuration

Note: This urls.py is the root route entry (the master entry).

The first static route urls demo method:

From hello import views urlpatterns = [path('admin/', admin.site.urls), # default path('hello/', views.index), # new]Copy the code

Log in the browser, type http://192.168.8.130:8888/hello/, the effect is as follows:

Second way to demonstrate static route urls:

From Django. urls import path,include # Import the include module urlpatterns = [path('admin/', admin.site.urls), # default path('hello/', include('hello.urls'))), # new]Copy the code

Log in the browser, type http://192.168.8.130:8888/hello/hello, the effect is as follows:


If you like my article, welcome to pay attention to my public number: drip technology, scan code attention, share irregularly