django基础《一》知识点整理

内容整理
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 func(request):
                # request.method   GET / POST

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

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


                # return HttpResponse("字符串")
                # return render(request, "HTML模板的路径")
                # return redirect('/只能填URL')
8、模板渲染
    特殊的模板语言

        -- {{ 变量名 }}

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


            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': ['alex','eric'], 
                            'user_dict': {'k1': 'v1', 'k2': 'v2'}})


            index.html

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

                    <a> {{ user_list.1 }} </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': ['alex','eric'], 
                            'user_dict': {'k1': 'v1', 'k2': 'v2'}})


            index.html

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

                    <a> {{ user_list.1 }} </a>
                    <a> {{ user_dict.k1 }} </a>
                    <a> {{ user_dict.k2 }} </a>

                    {% if age %}
                        <a>有年龄</a>
                        {% if age > 16 %}
                            <a>老男人</a>
                        {% else %}
                            <a>小鲜肉</a>
                        {% endif %}
                    {% else %}
                        <a>无年龄</a>
                    {% endif %}
                </body>

            </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值