潭州课堂25班:Ph201805201 django框架 第九课 模型补充 博客小案例 (课堂笔记)

聚合查询:

 

分组查询: annotate() 方法

例:查询某学院学生人数,(一对多查询)

以字典的形式输出

 annotate(统计 ‘关联学生字段 出现的次,).字典形式(键,值)

例:查询每项课程有多少学生 (多对多查询)

先拿到所有课程信息表,

annotate(统计 ‘dept’ 出现的次,).字典形式(键,值)

 F 查询:

Q 查询:

连接到阿里云服务器,上传项目,在该项目中创建新的 APP,

创建好后在本地更新下载下,

在配置文件中注册 APP

分配 URL

更新下

建表

 

生成执行文件

提交到数据库

 编辑函数

from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse
from .models import *

# Create your views here.

# 主页面
def index(request):
    return render(request,'blog/index.html')

# 添加页面
def add (request):
    # 第一次进来是 GET 请求,点提交时是 PST 请求
    if request.method == 'GET':
        return render(request,'blog/add.html')
    elif request.method == 'POST':
        #  从前台传来的数据
        title = request.POST.get('title')
        content = request.POST.get('content')
        # 保存到数据库
        blog = Blog(title=title,content=content)
        blog.save()
        # 重定向
        return redirect(reverse('add'))

# 文章列表
def list(request):
    #  查数据
    blog_list = Blog.objects.all()
    #                                       传到前台
    return render(request,'blog/list.html',context={'blog_list':blog_list})

# 文章详情页面
def detail(request,blog_id):
    blog = Blog.objects.get(id=blog_id)
    return render(request,'blog/detail.html',context={'blog':blog})

# 编辑数据
def edit(request,blog_id):
    blog = Blog.objects.get(id=blog_id)
    if blog:
        if request.method == 'GET':
            # 将数据传到框框内
            return render(request,'blog/add.html',context={'title':blog.title,
                                                       'content':blog.content})
        else:
            # 将传回的新数据保存到数据库
            title = request.POST.get('title')
            content = request.POST.get('content')
            blog.title = title
            blog.content = content
            blog.save()
            return redirect(reverse('add'))
    else:return HttpResponse('没有这篇博客')

# 删除数据
def delete(request,blog_id):
    blog = Blog.objects.get(id=blog_id)
    if blog:
        blog.delete()
        return redirect(reverse('list'))
    else:return HttpResponse('没有这篇博客')

  

视图函数:

 html :

视图:

 

 html

 视图:

 

 html

 

视图:

原文地址:https://www.cnblogs.com/gdwz922/p/9950282.html