Django笔记1

内容整理
    1.创建django工程
        django-admin startproject 工程名
    2.创建APP
        cd 工程名
        python manage.py startapp cmdb

    3.静态文件
        project.settings.py

        STATICFILES_dirs = {
            os.path.join(BASE_DIR, 'static'),
        }

    4.模板路径
        DIRS ==>  [os.path.join(BASE_DIR, 'templates'),]

    5.settings中
        middlerware
            注释csrf

    6.定义路由规则
        url.py
            'login' --> 对应一个函数名
    7.定义视图函数,
        app下views.py
            def login(request):
                #request.method:获取用户传的方法 GET 或 POST
                #http://127.0.0.1:8001/home?nid=123&name=alex
                #request.GET.get('',None)  #获取请求发来的数据

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

                #return HttpResponse('字符串')
                return render(request, 'HTML模板的路径')
                return redirect('URL路径')  #只能传url路径 如果return redirect('/home') /代表本地地址

    8.模板渲染
        特殊的模板语言
        def func(request):
            return render(request,'index.html',{'current_user':"alex"})

        index

        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':['alex','eric]})

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

                        <ul>
                            {%for row in user_list %}
                                <li>{{ row }}<li>
                            {% endfor %}
                        <ul>
                    <body>
                 html

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

                html
                <body>
                    <a>{{ user_list.1 }}<a>
                    <a>{{ user_dict.k2 }}<a>
                    <a>{{ user_dict.k1 }}<a>
                <body>
                html


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

                html
                <body>
                    <a>{{ user_list.1 }}<a>
                    <a>{{ user_dict.k2 }}<a>
                    <a>{{ user_dict.k1 }}<a>

                    {%if age%}
                        <a>有年龄<a>
                        {% if age > 18 %}
                            <a>老孩子<a>
                         {% else %}
                            <a>小孩子<a>
                        {% endif %}
                     {% else %}
                        <a>无年龄<a>
                    {% endif %}

                <body>
                html    

 

posted @ 2018-06-08 13:34  想翻身的猫  阅读(128)  评论(0编辑  收藏  举报