第二单元知识点

1. 相当于mysql中创建数据表

2. 配置数据库  settings.py

3. 编写模型类  相当于 建表

   ```
   create table 表名(
       字段 类型 约束
       ....
   )
   编写 学生模型类   模型类的类名就相当于是表名
   姓名 年龄
   ```

4. 生成迁移文件   python manage.py makemigrations

   - Did you install mysqlclient?

     1. 在项目同名目录下的__init__.py文件中加入2句话

        ```py
        import pymysql
        pymysql.install_as_MySQLdb()
        ```

   - 1049, "Unknown database 'django0617'"

     1. 在mysql中创建对应的数据库   create database 数据库名 charset utf8;

   - No changes detected

     1. 要注册app

5. 执行迁移文件

   python manage.py migrate

1. 写模型类  相当于创建表
   - 注册app
   - 配置数据库信息
   - 在models.py中编写模型类
   - 生成迁移文件   python manage.py makemigrations
   - 执行迁移文件   python manage.py migrate
   - 向表中添加数据
     1. 创建超级用户  python manage.py createsuperuser
     2. 自己输入用户名
     3. email可以直接敲回车
     4. 输入密码:密码就是没反应   如果密码少于8个字符 会警告
     5. 确认密码
     6. successfully   成功
     7. 重新启动项目   路由直接访问admin
     8. 输入刚刚自己创建的超级用户 登录即可

#### 总结流程

1. 创建django项目

2. 创建django应用

3. 配置文件

   - 注册app:如果不注册 迁移报错

     ```python
     INSTALLED_APPS = [
         'myapp'  # 注册app
     ]
     ```

   - 配置数据库

     ```python
     DATABASES = {
         'default': {
             'ENGINE': 'django.db.backends.mysql',
             'NAME': "sjk19",  # 数据库名
             "PASSWORD":"root",  # 密码
             "HOST":"localhost", # 主机
             "USER":"root",      # 用户
             "PORT":3306        # 端口
         }
     }
     冒号前边大写 端口是整型
     ```

   - 配置模板目录

4. 编写模型类

   ```python
   from django.db import models
   # Create your models here.
   class Tea(models.Model):
       name = models.CharField(max_length=10,verbose_name="姓名")
       # 指定表名  定义元类
       class Meta:
           db_table = "tea"
       # 返回对象的描述信息
       def __str__(self):
           return self.name
   
   ```

5. 生成迁移文件

   ```python
   python manage.py makemigrations
   ```

6. 执行迁移文件

   ```python
   python manage.py migrate
   ```

7. 创建超级用户

   ```python
   python manage.py createsuperuser
   ```

8. 在admin.py中注册模型类

   ```python
   from django.contrib import admin
   from myapp.models import Tea
   # Register your models here.
   # 注册模型类
   admin.site.register(Tea)
   ```

9. 在admin管理后台中添加数据

10. 写视图

    ```python
    from django.shortcuts import render
    from myapp.models import Car
    # Create your views here.
    # 渲染模板目录
    def getindex(request):
        # 返回模板目录 如果info后边是从数据库中读取出来的数据就好了
        # info = {"name":"五菱宏光"}
        # 获取全部数据语法  模型类.objects.all()  等同于 select * from 表;
        cars = Car.objects.all()
        dict = {"info":cars}
        return render(request,"index.html",dict)
    ```

11. 模板渲染

    ```html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <table border="1px" width="500px" align="center">
            <tr>
                <th>名字</th>
                <th>颜色</th>
                <th>速度</th>
                <th>价格</th>
                <th>品牌</th>
            </tr>
            {% for foo in info %}
                <tr>
                    <th>{{ foo.name }}</th>
                    <th>{{ foo.color }}</th>
                    <th>{{ foo.speed }}</th>
                    <th>{{ foo.price }}</th>
                    <th>{{ foo.brand }}</th>
                </tr>
            {% endfor %}
    
        </table>
    </body>
    </html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值