程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python beginner

編輯:Python

List of articles

    • Get started quickly
    • python Version of the problem
    • Django Basics
      • 1.models.py
      • 2.views.py
      • 3.urls.py
      • 4. Call template :settings.py file ( The front and rear ends do not separate )
      • 5. Connect to database
      • establish django project
      • View version
      • matters needing attention
      • Service deployment uwsgi+nginx
      • Generate test data in batches

actually , When I wrote this article, I had really stepped into python Project oriented It has been more than a year since the gate of , But there is still a feeling of being a beginner , Feeling overwhelmed by the new environment and knowledge , endeavour . Last year I was very young , It's still . I don't know where the card is , Like a tree that grows very slowly , It's probably an environment driven personality .
Tell the truth , I am really at a loss for the future , The reason may be that we didn't make enough preparations , But you still have to smile easily in front of others , I can't let others worry about me ? I had to try my best to clear up my original mood , The feeling of ignorance at the beginning ( wry smile )

There is no particular vision , I just hope everything goes smoothly

Get started quickly

mkdir Folder
git clone Warehouse url
# Create a virtual environment and specify PYTHON edition 
# 2.7
virtualenv venv --python=python2.7
# 3.6
# Method 1
virtualenv venv --python=python3.6
# Method 2
python3 -m venv venv(2、3 Environmental coexistence )
perhaps
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python manage.py runserver 0:9900

python Version of the problem

When two versions of a host exist at the same time python(2.7&3.8) when , Generally, the system PATH The one configured in
however

 Use pip and python Command wake up 2.7, Use pip3 and python3 Command wake up 3.8
2.7python With all versions mysql adapter , And adaptation django1.10
3.6python With all versions django Adaptation and adaptation mysql8.0

Django Basics

1.models.py

The structure class in which the table is written function

python manage.py makemigrations app name
python manage.py migrate app name

Generate data table

2.views.py

By importing models.py File to create data
from blog import models
def db_handle(request):
models.UserInfo.objects.create(username=‘andy’,password=‘123456’,age=33)
return HttpResponse(‘OK’)
Modifying data :filter(id=1).update(age=18)# find id=1 The data of , take age Change it to 18

3.urls.py

Configure the routing , visit views.py file
from blog import views
urlpatterns = [
url(r’^admin/’, admin.site.urls),
url(r’^test’, views.db_handle),==> Point to test The routing , Jump to views.db_handle Method , perform t1.html page
# notes :lrhold/abc It represents the... Under the root directory abc file , and domain/abc/ refer to abc In the catalog default file , With or without slashes is often used in SEO Optimize
]

4. Call template :settings.py file ( The front and rear ends do not separate )

Path to the configuration template
TEMPLATES=[
{

‘DIRS’: [os.path.join(BASE_DIR,‘templates’)],# Configure template path
},]

5. Connect to database

pip install PyMySQL
modify : The configuration file is the same as that in the directory init.py
import pymysql
pymysql.install_as_MySQLdb()
perhaps
pip install mysqlclient

Method 1.
modify settings Medium DATABASES Variable

Method 2.
The database configuration is stored in mysql.cnf
stay settings I quote :

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATABASES = getDatabase(BASE_DIR)
import platform
import os
def getDatabase(base_dir):
p = platform.system()
if p == 'Windows':
return {

'default': {

'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {

"read_default_file": os.path.join(base_dir, 'mysqltest.cnf'),
}
}
}
else:
return {

'default': {

'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {

"read_default_file": os.path.join(base_dir, 'mysql.cnf'),
}
}
}

establish django project

pip3 install django== Version number
django-admin startproject blog_project ==》cd( Be careful not to. .py)
python manage.py startapp blog
python manage.py runserver 8000

View version

python -m django --version

matters needing attention

1.django When connecting to the database, the page displays url Variable mismatch : take url The first variable is null

urlpatterns = [
url('^', admin.site.urls),
url('^', index, name='index'),
]

2. To configure settings.py==>INSTALLED_APP add to app name
3. To configure settings.py==>ALLOWED_HOSTS = [‘10.xxx.xxx.xxx’,]
4. Use vscode Of sftp Connect remote linux Prompt permission error , Should be rolled back vscode Version to 1.34
5.windows No, python-dev package , Nor in windows Upper Department ngnix+uswgi The server ,pip Installation will fail

Service deployment uwsgi+nginx

1. start-up :uwsgi --ini uwsgi.ini
2. Write a ngnix.conf Put under Project , Put this conf Soft link or move to /etc/nginx/sites-enabled/ below , Last service nginx reload

[uwsgi.ini]
# Use 9100 port 
http= :9100
#socket = :9100
# Project location mysite It's my project name 
chdir = /home/liting/projects/demand-management/logsites
# Change it to your own project name 
module = logsites.wsgi
# Virtual environment Directory env Is my server virtual environment name 
home = /home/liting/workspace/Envs/env3
master = true
# The biggest working process , Generally, it is set according to the number of cores , It can also be set arbitrarily 
processes = 4
vacuum = true
# Log files 
daemonize = /home/liting/projects/demand-management/logsites/uwsgi.log
# Static file directory 
static-map = /static=/home/liting/projects/demand-management/logsites/static
pidfile=uwsgi.pid
daemonize=uswgi.log
[nginx.conf]
server {

listen 80;
server_name 10.xxx.xxx.xxx:9100;
charset utf-8;
client_max_body_size 75M;
# Change to your media file directory 
location /media {

alias /home/liting/projects/demand-management/logsites/media; # your Django project's media files - amend as required
}
# Change to your static file directory 
location /static {

alias /home/liting/projects/demand-management/logsites/static; # your Django project's static files - amend as required
}
location / {

# This is the top uwsgi.ini Set up 8080 port , Be consistent ,nginx Even with this and uwsgi communication 
uwsgi_pass 10.xxx.xxx.xxx:9100;
include /etc/nginx/uwsgi_params;
}
}

Generate test data in batches

Reference resources https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/80/


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved