• django笔记补充


    安装 pip install django
    环境变量: C:Program FilesAnaconda3Scripts

    django-admin startproject mysite 创建django工程
    mysite目录
        -mysite            #对整个程序进行配置
            - __init__
            -settings      #配置文件
            -urls           #URL对应关系
            -wsgi           #规则 接口 用于帮组django创建socket 遵循wsgi规范 正式用的是uwsgi+nginx
        -manage.py   #管理django程序
                       - python manage.py 运行django
                       - python manage.py startapp  创建app
                       - python manage.py makemigrations orm框架 自动生成数据库 操作数据库
                       - python manage.py migrate  orm框架 自动生成数据库 操作数据库

     python.exe manage.py runserver 127.0.0.1:8000 启动django web页面

     支持wsgi接口的模块:
        server_names = {
        'cgi': CGIServer,
        'flup': FlupFCGIServer,
        'wsgiref': WSGIRefServer,
        'waitress': WaitressServer,
        'cherrypy': CherryPyServer,
        'paste': PasteServer,
        'fapws3': FapwsServer,
        'tornado': TornadoServer,
        'gae': AppEngineServer,
        'twisted': TwistedServer,
        'diesel': DieselServer,
        'meinheld': MeinheldServer,
        'gunicorn': GunicornServer,
        'eventlet': EventletServer,
        'gevent': GeventServer,
        'geventSocketIO':GeventSocketIOServer,
        'rocket': RocketServer,
        'bjoern' : BjoernServer,
        'auto': AutoServer,
    }

    ORM:关系对象映射

    chouti
        -chouti
            -配置
        -主站 app
        -后台管理 app

    #创建app
    python manage.py startapp cmdb
    python manage.py startapp openstack
    python manage.py startapp moniter

    app:
        migrations 数据库操作记录 修改表结构的记录
        admin  django提供的后台管理
        建表会报错参照 https://blog.csdn.net/jiangxunzhi123/article/details/86160146
        apps   配置当前app
        models  ORM,写指定的类,通过命令可以创建数据库结构
        tests  单元测试
        views   业务代码


    1,配置模板的路径
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')]
            ,
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]


    2,配置静态目录:
    STATIC_URL = '/static/'
    STATICFILES_DIRS=(
      os.path.join(BASE_DIR,'static'),
    )

    <link rel="stylesheet" href="/static/commons.css"/>
    <script src="/static/jquery-1.12.4.js"></script>


    url提交是get
    submit提交是post

    settings中
        middlerware
            #注释csrf

    GET:获取数据
    POST:提交数据


    定义路由规则:
    url.py
        "login"  --->函数名


    定义视图函数
        app下的views.py
        def func(request):
            #request.method  GET/POST

            #http://127.0.0.1:8000/home?nid=123&name=alex
            #request.GET.get('',None)  #获取请求发来的数据

            #request.POST.get('',None)

            #return HttpResponse("字符串")
            #return render(request,"Html模板的路径")
            #return redirect('/只能填url')


    模板渲染
       特殊的模板语言
       --{{变量名}}

        def func(request):
            return render(request,'index.html',{'current_user':"alex"})

        index.html
        <html>
            <body>
                <div>{{current_user}}</div>
            </body>
        </html>

        ===>最后生成的字符串
        <html>
            <body>
            <div><alex></div>
            </body>
        </html>

        ---->For循环
           def func(request):
            return render(request,'index.html',{'current_user':"alex",'user_list':['lei','leo']})

        index.html
        <html>
            <body>
                <div>{{current_user}}</div>

                <ul>
                    {% for row in user_list%}
                        {% if row=="alex"%}
                        <li>{{row}}</li>
                            {%endif%}
                     {%endfor%}
                </ul>
            </body>
        </html>



       索引
          def func(request):
            return render(request,'index.html',{'current_user':"alex",
                                                'user_list':['lei','leo'],
                                                'user_dict':{'k1':'v1','k2':'v2'}})

        index.html
        <html>
            <body>
                <div>{{current_user}}</div>
                <a>{{user_list.0}}</a>
                <a>{{user_dict.k1}}</a>
                <a>{{user_dict.k2}}</a>
            </body>
        </html>



       条件
        def func(request):
            return render(request,'index.html',{'current_user':"alex",
                                                'age':18,
                                                'user_list':['lei','leo'],
                                                'user_dict':{'k1':'v1','k2':'v2'}})

        index.html
        <html>
            <body>
                <div>{{current_user}}</div>
                <a>{{user_list.0}}</a>
                <a>{{user_dict.k1}}</a>
                <a>{{user_dict.k2}}</a>

                {% if age %}
                    <a>有年龄</a>
                    {% if age > 19 %}
                        <a>老了</a>
                    {%else%}
                        <a>小鲜肉</a>
                    {%endif%}
                { else}
                   <a>false</a>
                {%endif%}


            </body>
        </html>

    django请求生命周期
        用户--->URL对应关系(匹配) -->视图函数 -->返回给用户(字符串)
        用户--->URL对应关系(匹配) -->视图函数 -->打开一个HTML文件,读取内容


    作业:
    XXOO管理:
        mysql
        sqlalchemy
        主机管理表:
            Ip
            端口
            业务线
            ...

        用户表:
            用户名
            密码

        功能:
            1,登录
            2,主机管理页面
                -查看所有的主机信息(4列)
                -增加主机信息(8列) ** 模态对话框
            3,查看详细
                url:
                    "detail" -->detail
                def detail(request):
                    nid=request.GET.get("nid")
                    v=select * from tb where id=nid;
                    ...

            4,删除
                del_host ->delete _host
                def delete_host(request):
                    nid=request.POST.get('nid')
                    delete from tb where id=nid
                    return redirect('/home')

  • 相关阅读:
    一种client同步server数据的方案
    nodejs package.json解释
    node.js JS对象和JSON字符串之间的转换
    setInterval的用法
    ActiveMQ 入门Nodejs版
    ActiveMQ + NodeJS + Stomp 极简入门
    为什么 ++[[]][+[]]+[+[]] = 10?
    Child Process模块
    phantomjs 解码url
    PhantomJSのメモいろいろ
  • 原文地址:https://www.cnblogs.com/leiwenbin627/p/10981013.html
Copyright © 2020-2023  润新知