Python圈Python基础

Django基础4--Shell及管理页面

2020-08-26  本文已影响0人  伊洛的小屋
1.Django Shell
(django) ➜  AutoPlatform python manage.py shell
Python 3.7.5 (default, Nov 29 2019, 14:32:46) 
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

from django.db import models

# Create your models here.


class ProjectInfo(models.Model):
    """ 项目信息模型 """
    project_name = models.CharField(max_length=30)
    dev = models.CharField(max_length=20)
    tester = models.CharField(max_length=20)
    add_data = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        # 返回项目名称
        return self.project_name
>>> from autoapi.models import ProjectInfo
>>> ProjectInfo.objects.all()
<QuerySet []>

2.Django管理页面

Django提供的管理网站(admin site)让你能够轻松地处理模型。网站的管理员可使用管理网站,但普通用户不能使用

(django) ➜  AutoPlatform python manage.py createsuperuser

这里的username设置为admin, 密码也设置为admin

Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
3.查看Django管理页面
# 作者:伊洛Yiluo 公众号:伊洛的小屋
# 个人主页:https://yiluotalk.com/
(django) ➜  AutoPlatform python manage.py runserver 
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 25, 2020 - 15:12:38
Django version 3.1, using settings 'AutoPlatform.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
4. 进入到管理站点页面
5.管理后台页面加入应用

我们创建的模型,必须手工进行注册

from django.contrib import admin
from .models import ProjectInfo

# Register your models here.

admin.site.register(ProjectInfo)

(django) ➜  AutoPlatform python manage.py shell
Python 3.7.5 (default, Nov 29 2019, 14:32:46) 
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from autoapi.models import ProjectInfo
>>> ProjectInfo.objects.all()
<QuerySet [<ProjectInfo: 接口自动化>]>
>>> 

和上面最开始相比,已经有了数据

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读