zoukankan      html  css  js  c++  java
  • app view

    from django.shortcuts import render,HttpResponse
    from .models import ProductModel

    # Create your views here.
    # 所有的请求信息,都在request对象,视图函数的参数必须要有request
    def index(request):
    return HttpResponse('<h1>商城</h1>')


    def produectList(request):
    return HttpResponse('<h1>产品列表</h1>')


    def userLogin(request):
    print(request.GET.get('username'))
    print(request.GET.get('password'))
    return HttpResponse('登录')

    def login(request):
    return render(request,'form.html',{'form':'登录页面'})

    def product(request):
    p1 = ProductModel.objects.get(pk=2)
    alllist = ProductModel.objects.all()
    print(alllist)
    dictObj = {
    'name':p1.name,
    'brief':p1.brief,
    'productImg':p1.prodectImg,
    'alllist':alllist,
    'content':'<h1>图片</h1>'

    }
    #第三个参数就是传入变量的字典
    return render(request,'index.html',dictObj)

    def addProduct(request):
    #1
    # p1 = ProductModel(name='葡萄',brief='这是葡萄产品')
    # p1.save()
    #2
    # dictobj = {'name': '芒果', 'brief': '这是芒果'}
    # ProductModel.objects.create(**dictobj)
    #3
    ProductModel.objects.create(name='苹果',brief="这是香瓜")

    return HttpResponse('添加数据到数据库')


    def getProduct(request):
    #查找这表所有信息
    product = ProductModel.objects.all()
    #查找表中某一列的数据
    productname = ProductModel.objects.all().values('name')
    #查找表中某几列的数据
    productInfo = ProductModel.objects.all().values_list('name','brief')
    #查找某一行符合查询条件的结果
    # apple = ProductModel.objects.get(name='苹果')
    #查找符合条件的所有结果
    # apples = ProductModel.objects.filter(name='苹果', id=1)
    #查找不符合条件的所有结果
    # others = ProductModel.objects.exclude(name='苹果')
    #pk==>primary key就代表主键的意思,也就是ID
    # apples = ProductModel.objects.filter(pk=1)
    #获取id大于1#id__gt=1
    apples = ProductModel.objects.filter(id__gt=2)
    #获取id大于等于1#id__gte=1
    apples = ProductModel.objects.filter(id__gte=2)
    #获取id小于1#id__lt=10
    apples = ProductModel.objects.filter(id__lt=2)
    #获取id小于等于#id__lte=10
    apples = ProductModel.objects.filter(id__lt=4)
    #id__in=[11, 22, 33] #获取id等于11、22、33的数据
    #在某个列表范围内,或者使用或来进行相关的查询的时候可以使用
    apples = ProductModel.objects.filter(name__in=['苹果','香蕉'])
    #模糊查找==》contains
    ## 包含 name__contains="ven" icontains大小写不敏感
    apples = ProductModel.objects.filter(brief__contains='这是')



    #id__range=[1, 2]在什么范围
    apples = ProductModel.objects.filter(id__range=[2,4])




    # order by排序.order_by('id').order_by('-id')
    apples = ProductModel.objects.filter(id__range=[2, 4]).order_by('-id')




    # limit 、offset====》objects.all()[10:20]---100条结果,page页的内容,每页num条信息
    # result[page-1*num,page*num]

    apples = ProductModel.objects.filter(id__range=[2, 4]).order_by('-id')[0:2]




    print(apples)
    return HttpResponse('aaaa')


    def updateProduct(request):
    #1
    ProductModel.objects.filter(id__range=[2, 4]).order_by('-id').update(name='花甲')

    #2
    obj = ProductModel.objects.filter(id=6).order_by('-id')[0]
    obj.name = '520'
    obj.save()
    return HttpResponse('修改产品')


    def deleteProduct(request):
    obj = ProductModel.objects.filter(id=6).delete()
    return HttpResponse('删除对象')



    import json
    def jsonApi(request):
    p1 = ProductModel.objects.get(pk=2)
    dictObj = {
    'name': p1.name,
    'brief': p1.brief,
    'productImg': str(p1.prodectImg),
    'content': '<h1>图片</h1>'
    }

    print(request.GET.get('username'))

    jsonStr = json.dumps(dictObj,ensure_ascii=False)
    result = HttpResponse(jsonStr)
    result['Access-Control-Allow-Origin'] = '*'
    result['Access-Control-Allow-Headers'] = "Content-Type"

    return result



    def ajaxPage(request):
    return render(request,'ajax.html')

  • 相关阅读:
    本月周六周日LIST集合
    c#动态调用WEBSERVICE接口
    c#调用
    web上传下载文件
    MVC 的知识
    MongoDB 无法创建抽象类的问题,
    并行活动
    C# 字符串计算表达式
    c# 将字符串转换为逻辑表达式(字符串转换布尔)
    C# 中间语言、CLR、CTS、CLS
  • 原文地址:https://www.cnblogs.com/daicw/p/15404253.html
Copyright © 2011-2022 走看看