Python—Django应用

1、概念简介

Django是Python的 Web框架;

web开发中前后端分离已经成为行业标准,前后端划分的界限:
前端:一般是值用Html+CSS+JS来实现的部分;
后端:在服务器端运行的使用 java、python、c等语音开发的部分;

CDN:全称是Content Delivery Network,即内容分发网络。

2、环境搭建

2.1、创建项目

官方教程:https://docs.djangoproject.com/en/4.1/intro/

(一)、模块安装

pip install django

(二)、项目创建

在路径:E:\PythonProject\webpro 下执行:
django-admin startproject mysite

当前目录结构:

├── mysite
│   ├── mysite
│   │   ├── __init__.py
│   │   ├── asgi.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── manage.py

(三)、配置库初始化

PS E:\PythonProject\webpro\mysite> python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
PS E:\PythonProject\webpro\mysite> 

(四)、启动服务

  • python manage.py runserver localhost:8000
PS E:\PythonProject\webpro\mysite> python manage.py runserver localhost:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 16, 2022 - 16:06:31
Django version 4.1, using settings 'mysite.settings'
Starting development server at http://localhost:8000/
Quit the server with CTRL-BREAK.

说明:此处启动服务时,用户可以自定义服务端口;

(五)迁移配置库
修改 settings.py 里的 数据库信息,改为如下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxx',
        'HOST': '127.0.0.1',
    }
}
  • 安装 mysqlclient 模块
    pip install mysqlclient

  • 执行配置迁移
    PS E:\PythonProject\webpro\mysite> python manage.py migrate
    完成后mysql 的库中会有如下表:

mysql> 
mysql> use django;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_django           |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)

mysql> 

(六)、重启服务
执行步骤 (四),如果可以正常出现如下界面,则数据库配置迁移成功;
在这里插入图片描述

2.2、创建应用

项目和应用的区别:
应用程序是执行某些操作的Web应用程序;
项目是特定网站的配置和应用程序的集合。一个项目可以包含多个应用。一个应用可以位于多个项目中。

在路径:E:\PythonProject\webpro\mysite 下执行:
python manage.py startapp polls

后续步骤:
1)在 polls下新建 views 和 urls;
2)在 mysite 的 urls 里添加 polls 的 urls;

2.3、Admin简介

在项目根目录执行如下命令,创建管理员账号:
python manage.py createsuperuser

启动服务,访问地址:http://127.0.0.1:8000/admin/将出现如下界面:
在这里插入图片描述

3、开发实践

3.1、创建模型

=========================================== end ============================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值