Django(二)

1.django框架

1.1 安装

python 复制代码
pip install django==3.2

1.2 命令行

  • 创建项目
python 复制代码
cd 指定目录
django-admin startproject 项目名
python 复制代码
mysite
├── manage.py              [项目的管理工具]  
└── mysite
    ├── __init__.py
    ├── settings.py        【配置文件,只有一部分。程序启动时,先读取django内部配置,再读settings.py】
    ├── urls.py			   【主路由,在里面编写  /xxx/xxx/xxx ---> index 】
    ├── asgi.py            【异步】
    └── wsgi.py            【同步,主】
python 复制代码
from django.contrib import admin
from django.urls import path

from django.shortcuts import HttpResponse

def info(request):
    print("请求来执行了")
    return HttpResponse("xxxx")

def xxxx(request):
    print("请求来执行了")
    return HttpResponse("。。。。。。")

urlpatterns = [
    # path('admin/', admin.site.urls),
    path('api/index/', info),
    path('api/show/', xxxx),
]
  • 运行
python 复制代码
cd 项目
python3.9 manage.py runserver
python3.9 manage.py runserver 127.0.0.1:8000
python3.9 manage.py runserver 127.0.0.1:9000
  • app概念
python 复制代码
cd 项目
python manage.py startapp 名字
python 复制代码
mysite
├── manage.py              [项目的管理工具]  
├── web
    ├── __init__.py
    ├── views.py           [视图函数]
    ├── models.py          [ORM,基于models可以对数据库进行简便的操作]
    ...
└── mysite
    ├── __init__.py
    ├── settings.py        【配置文件,只有一部分。程序启动时,先读取django内部配置,再读settings.py】
    ├── urls.py			   【主路由,在里面编写  /xxx/xxx/xxx ---> index 】
    ├── asgi.py            【异步】
    └── wsgi.py            【同步,主】
python 复制代码
mysite
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── web
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

1.3 Pycharm


python 复制代码
django-admin startproject 项目名称

cd 项目目录
python manage.py startapp
python manage.py runserver

2. 虚拟环境

2.1 创建虚拟环境 - 命令行

  • venv,Python官方用于创建虚拟环境的工具。
python 复制代码
cd xxx/xxx/crm
python3.9 -m venv ddd
python3.7 -m venv xxxx
python3.7 -m venv /xxx/xxx/xxx/xx/ppp
  • virtualenv 【推荐】
python 复制代码
pip install virtualenv
python 复制代码
cd /xxx/xx/
virtualenv ddd --python=python3.9
python 复制代码
virtualenv /xxx/xx/ddd --python=python3.7

操作:

  • F:\envs\ 创建虚拟环境。
python 复制代码
cd F:\envs
virtualenv crm --python=python3.9
  • 激活虚拟环境
  • win
python 复制代码
cd F:\envs\crm\Scripts
activate
  • mac
python 复制代码
source /虚拟环境目录/bin/activate
  • 安装包
python 复制代码
pip install 包名
  • 创建django项目 `D:\project\crm
python 复制代码
cd D:\project
django-admin startproject crm
python 复制代码
D:\project\crm
├── manage.py              [项目的管理工具]  
└── crm
    ├── __init__.py
    ├── settings.py        【配置文件,只有一部分。程序启动时,先读取django内部配置,再读settings.py】
    ├── urls.py			   【主路由,在里面编写  /xxx/xxx/xxx ---> index 】
    ├── asgi.py            【异步】
    └── wsgi.py            【同步,主】
python 复制代码
python manage.py startapp xxxx
python manage.py runserver 
  • 退出虚拟环境
python 复制代码
deactivate

2.2 Pycharm项目+虚拟环境


.venv:隐藏文件夹

  • 在虚拟环境中安装 requests
python 复制代码
pip install requests

2.3 django+虚拟环境【最新】

python 复制代码
pip install django


注意:创建django最新版可以。

2.3.1 django+虚拟环境【指定版本】

python 复制代码
pip install django==3.2
python 复制代码
django-admin startproject Django
这么命令会把Django项目嵌套着多了一层目录放进去,不是我们想要的
django-admin startproject Django .
我们想要的是将manage.py和Django这个目录放在当前这个项目目录



相关推荐
短剑重铸之日21 分钟前
《7天学会Redis》Day 4 - 高可用架构设计与实践
数据库·redis·缓存
NineData42 分钟前
第三届数据库编程大赛-八强决赛成绩揭晓
数据库·算法·代码规范
難釋懷1 小时前
认识Redis
数据库·redis·缓存
超级种码1 小时前
Redis:Redis脚本
数据库·redis·缓存
想唱rap1 小时前
表的约束条件
linux·数据库·mysql·ubuntu·bash
超级种码1 小时前
Redis:Redis 命令详解
数据库·redis·bootstrap
qq_401700411 小时前
Qt 事件处理机制
java·数据库·qt
Elastic 中国社区官方博客2 小时前
使用 jina-embeddings-v3 和 Elasticsearch 进行多语言搜索
大数据·数据库·人工智能·elasticsearch·搜索引擎·全文检索·jina
深海小黄鱼2 小时前
mysql 导入csv文件太慢, Error Code: 1290.
数据库·mysql
小宇的天下2 小时前
Calibre Connectivity Extraction(21-1)
数据库·oracle