Python后端之Django


title: mysql学习笔记
renderNumberedHeading: true
grammar_cjkRuby: true


远程连接
mysql -u root -h **.**.**.** -p
DDL
-create drop alter

DML
-insert delete update

DQL

-select

DCL

创建UTF-8编码数据库
create database django1906 default charset utf8;

注意此处不能在配置/etc/my.cnf添加skp-grant-table否则会创建用户失败
创建mysql用户操作指定表
create user 'django'@'localhost' identified by '123456';
赋予指定权限
grant insert,delete,update,select,create,alter on django1906.* to 'django'@'localhost';


title: python后端Django之models操作
tags: python,Django,models
renderNumberedHeading: true
grammar_cjkRuby: true


-- 创建数据库
create database django1906 default charset utf8;
-- 创建用户
create user 'jackfrued'@'%' identified by '123456';
-- 给用户授权
grant all privileges on django1906.* to 'jackfrued'@'%';
flush privileges;

model创建:

class Subject(models.Model):
    """
    学科模型类
    """

    no = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    intro = models.CharField(max_length=1000)

    """
    为模型类添加新的附加内容
   """
    class Meta:
        db_table = 'tb_subject'

setting.py中添加配置
[图片上传失败...(image-51f946-1578307857925)]

python manage.py makemigrations polls
python manage.py migrate
创建迁移是需要注意设置默认值或者允许为空

创建管理员平台
python manage.py createsuperuser
http://127.0.0.1:8000/admin
[图片上传失败...(image-15dc1f-1578307857925)]
进入管理员平台后需要在admin.py注册
[图片上传失败...(image-3f160-1578307857925)]
之后显示
[图片上传失败...(image-858869-1578307857925)]

localhost:8000/admin中的Subject英文显示可以在Moldel.py中修改模型附加信息如:
[图片上传失败...(image-f17aec-1578307857926)]

单数形式verbose_name
复数形式verbose_name_plural

由于显示英文可以修改admin.py中的内容
[图片上传失败...(image-433d3a-1578307857926)]
新建模型对应的admin类继承ModelAdmin类
list_display #需要展示的内容
serch_fileds #可搜索的内容选项
ordering #排序规则与排序优先级
list_display_links #修改字段可以时点击的链接位置

model新增字段时
先添加字段然后---
title: python后端Django之页面views
tags: python,Django,views
renderNumberedHeading: true
grammar_cjkRuby: true


正确建立连接需要三个步骤
第一步在templates中建立相应页面
[图片上传失败...(image-4394a7-1578307857926)]
第二步在views.py中写请求处理函数

def index(request:HttpRequest):
    from polls.models import Subject
    query1 = Subject.objects.all()
    return render(request, 'index.html', context={
        'subjects': query1,
    })


def show_teacher(request):
    """
    :param request:HttpRequest
    :return:
    显示指定学科的老师
    """
    sno = request.GET['sno']
    from polls.models import Subject, Teacher
    subject = Subject.objects.get(no=sno)
    teachers = Teacher.objects.filter(subject__no=sno)
    context = {
    'subject': subject,
    'teachers': teachers,汉书

    }
    return  render(
        request, 'teacher.html', context
    )
    

第三步在Urls.py中添加相应Path

from polls.views import index, show_teacher

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('teachers/', show_teacher),
]

python manage.py makemigrations polls
python manage.py migrate
然后在admin.py中list_display添加is_hot


title: python后端Django之页面views
tags: python,Django,views
renderNumberedHeading: true
grammar_cjkRuby: true


正确建立连接需要三个步骤
第一步在templates中建立相应页面
[图片上传失败...(image-41ba96-1578307857926)]
第二步在views.py中写请求处理函数

def index(request:HttpRequest):
    from polls.models import Subject
    query1 = Subject.objects.all()
    return render(request, 'index.html', context={
        'subjects': query1,
    })


def show_teacher(request):
    """
    :param request:HttpRequest
    :return:
    显示指定学科的老师
    """
    sno = request.GET['sno']
    from polls.models import Subject, Teacher
    subject = Subject.objects.get(no=sno)
    teachers = Teacher.objects.filter(subject__no=sno)
    context = {
    'subject': subject,
    'teachers': teachers,汉书

    }
    return  render(
        request, 'teacher.html', context
    )
    

第三步在Urls.py中添加相应Path

from polls.views import index, show_teacher

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('teachers/', show_teacher),
]

你可能感兴趣的:(Python后端之Django)