标签:表格 username view djang from 建表 pymysql python img
python -m django --version
django-admin startproject login_dome
cd login_dome/
django-admin startapp app01
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config', #注册app,不然Django不识别
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware', #暂时不启用
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')], #没有pycharm,手动写进去
'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',
],
},
},
]
mkdir login_dome/templates #没有templates目录,手动创建
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
'db2': { #两个app也可以设置两个数据库,暂时不考虑
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
create databases logindome;
#连接本地数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'logindemo',
'HOST':'192.168.234.133',
'PORT':'3306',
'USER':'root',
'PASSWORD':'mysql',
}
}
import pymysql ##连接mysql
pymysql.install_as_MySQLdb()
#定义表格式
from django.db import models
class User(models.Model):
username = models.CharField(max_length=32) #varchar(32)
password = models.CharField(max_length=32
#创建迁移文件,将表放入数据库中
(venv) E:\py27\login_dome>python manage.py makemigrations
Migrations for 'app01':
app01\migrations\0001_initial.py
- Create model User
# 根据迁移文件将表迁移到数据库
python manage.py migrate
标签:表格 username view djang from 建表 pymysql python img
原文地址:https://www.cnblogs.com/yihutu/p/11821781.html