• Django 学习 (第四部)


    1、Django请求的生命周期
         首先是url---->路由系统 -> 试图函数(获取模板+数据=》渲染) -> 字符串返回给用户
    2、路由系统{建立路由关系urls.py (function base view ) }
    /index/ -> 函数或类.as_view()
    /detail/(d+) -> 函数(参数) 或 类.as_view()(参数)
    /detail/(?P<nid>d+) -> 函数(参数) 或 类.as_view()(参数)
    /detail/ -> include("app01.urls")
    /detail/ name='a1' -> include("app01.urls")
    - 视图中:reverse
    - 模板中:{% url "a1" %}

    {
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^business$', views.business),
    url(r'^host$', views.host),
    url(r'^test_ajax$', views.test_ajax),
    url(r'^app$', views.app),
    url(r'^ajax_add_app$', views.ajax_add_app),
    # url(r'^business_add', views.business),
    ]
    }


    3、视图{书写处理逻辑views.py (function base view)}

    FBV:函数function
    def index(request,*args,**kwargs):
    ..

    CBV:类class 一个class(相当于一个表)
    class Home(views.View):

    def get(self,reqeust,*args,**kwargs):
    获取用户请求中的数据:
    request.POST.get
    request.GET.get
    reqeust.FILES.get()

    # checkbox,
    ........getlist()

    request.path_info


    文件对象 = reqeust.FILES.get()
    文件对象.name
    文件对象.size
    文件对象.chunks()

    # <form 特殊的设置></form>


    给用户返回数据:
    render(request, "模板的文件的路径", {'k1': [1,2,3,4],"k2": {'name': 'aa','age': 73}})
    redirect("URL")
    HttpResponse(字符串)


    4、模板语言
    render(request, "模板的文件的路径", {'obj': 1234, 'k1': [1,2,3,4],"k2": {'name': 'aa','age': 73}})

    <html>

    <body>
    <h1> {{ obj }} </h1>
    <h1> {{ k1.3 }} </h1>
    <h1> {{ k2.name }} </h1>
    {% for i in k1 %}
    <p> {{ i }} </p>
    {% endfor %}

    {% for row in k2.keys %}
    {{ row }}
    {% endfor %}

    {% for row in k2.values %}
    {{ row }}
    {% endfor %}

    {% for k,v in k2.items %}
    {{ k }} - {{v}}
    {% endfor %}

    </body>
    </html>

    5、ORM(对象关系映射)
    a. 创建类和字段
    class User(models.Model):
    age = models.IntergerFiled()
    name = models.CharField(max_length=10)#字符长度

    Python manage.py makemigrations
    python manage.py migrate

    # settings.py 注册APP

    b. 操作


    models.User.objects.create(name='qianxiaohu',age=18)
    dic = {'name': 'xx', 'age': 19}
    models.User.objects.create(**dic)


    obj = models.User(name='qianxiaohu',age=18)
    obj.save()

    models.User.objects.filter(id=1).delete()

    models.User.objects.filter(id__gt=1).update(name='alex',age=84)
    dic = {'name': 'xx', 'age': 19}
    models.User.objects.filter(id__gt=1).update(**dic)

    models.User.objects.filter(id=1,name='root')
    models.User.objects.filter(id__gt=1,name='root')
    models.User.objects.filter(id__lt=1)
    models.User.objects.filter(id__gte=1)
    models.User.objects.filter(id__lte=1)

    models.User.objects.filter(id=1,name='root')
    dic = {'name': 'xx', 'age__gt': 19}
    models.User.objects.filter(**dic)

    v1 = models.Business.objects.all()
    # QuerySet ,内部元素都是对象

    # QuerySet ,内部元素都是字典
    v2 = models.Business.objects.all().values('id','caption')
    # QuerySet ,内部元素都是元组
    v3 = models.Business.objects.all().values_list('id','caption')

    # 获取到的一个对象,如果不存在就报错
    models.Business.objects.get(id=1)
    对象或者None = models.Business.objects.filter(id=1).first()


    外键:
    v = models.Host.objects.filter(nid__gt=0)
    v[0].b.caption ----> 通过.进行跨表






    外键:
    class UserType(models.Model):
    caption = models.CharField(max_length=32)
    id caption
    # 1,普通用户
    # 2,VIP用户
    # 3, 游客

    class User(models.Model):
    age = models.IntergerFiled()
    name = models.CharField(max_length=10)#字符长度
    # user_type_id = models.IntergerFiled() # 约束,
    user_type = models.ForeignKey("UserType",to_field='id') # 约束,

    name age user_type_id
    # aa 18 3
    # bb 18 2
    # cc 18 2




  • 相关阅读:
    【转载】消息队列使用的四种场景介绍
    Vue项目部署打包
    创建Vue项目vue-cli &#183; Failed to download repo vuejs-templates/webpack: connect ETIMEDOUT
    数据库错误:ORA-12154
    oracle数据库一条sql语句批量插入数据
    Linux time scap
    winscp不能使用root登录
    Linux find ./ -name *.bak | xargs rm -rf
    Linux命令echo 3 > /proc/sys/vm/drop_caches
    redis查看服务器占用端口
  • 原文地址:https://www.cnblogs.com/1314520xh/p/9005891.html
Copyright © 2020-2023  润新知