【django+python】web自动化平台搭建
django:开源的python开发框架,实现简单快捷的web网站开发,无需数据库就可以使用,自带sqlite,使用orm(对象关系映射),可以自动管理界面。
django环境搭建
安装pycharm、python3.8
安装django:
cmd:pip install django==3.8
(指定安装版本)
查看已安装的版本:pip show django
使用django构建项目:
确定项目地址:终端方式
执行命令:django-admin startproject xxx(项目名称)
验证创建成功:桌面上找这个文件
使用pycharm打开这个项目
启动django项目
终端启动
进入项目文件夹,执行启动命令:python manage.py runserver
查看原始的登录页面:http://127.0.0.1:8001/admin
配置settings.py
主要配置
BASE_DIR 项目地址
ALLOWED_HOSTS 允许访问的域名
DEBUG = True 生产环境下关闭debug
INSTALLED_APPS 注册应用
DATABASES 数据库
LANGUAGE_CODE = ‘zh-hans’ 界面语言中文
"""
Django settings for myproject project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. 项目地址
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! 密钥
SECRET_KEY = 'django-insecure-1*05(l7s6g!k9&@kq72@hpdozg(x(=ojh54!)qog$xf8tcmbx='
# SECURITY WARNING: don't run with debug turned on in production!
# 在生产环境下要手动关闭debug,否则信息会直接泄漏给用户
DEBUG = True
# 允许访问的域名 ip
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
# Application definition
# 注册应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
# 中间件 处理安全操作
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',
]
# url配置
ROOT_URLCONF = 'myproject.urls'
# 模板渲染
TEMPLATES = [
{
'BACKEND