1. Configuration environment
win10, python3.6, pycharm 2.
Life cycle of Django requests Client (user) -> URL correspondence (routing matching) -> view function or class (views) -> get the template (
templates) and data (models) for rendering -> return the client (user) string
, which is a typical MTV template
3. Creation of a Django project
Download :
pip3 install django
path (default in the python installation directory script): d: \python\Script\
cd d:\python\Script\ environment:
# Create Django project
django-admin startproject project name
# Run Django function
python manage.py runserver 127.0.0.1:8001 #Create
app
python manage.py startapp app name
Configuration template path:
in project.settings (templates are created by default with pycharm)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
编程开发 }, Configuration of static directory:
]
STATIC_URL = '/static/' in project.settings
STATICFILES_DIRS = (
os.path.join(BASE_DIR,"static"),
)
database creation: a, registered app
in project.app.models INSTALLED_APPS = [ 'django.contrib .admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', ] b , create database from django.db import models # app01_userinfo class UserInfo(models.Model):
# id column, auto increment, primary key
# username column, string type, specified length
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
c. Execute the command
python manage.py makemigrations
python manage .py migrate
********** Note *********** Django uses the MySQLdb module to link MySQL by default, and actively changes it to pymysql, and adds it to the __init__ file in the project folder with the same name The following code is enough:
import pymysql
pymysql.install_as_MySQLdb()