• 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    
  • 相关阅读:
    [iOS 主要框架的总结]
    [无线传感器 网络中的节点定位技术]
    [JAVA 多种方式读取文件]
    [IOS swift对比oc]
    [IOS 静态库]
    [U3D 导出Xcode工程包,用Xcode给U3D脚本传递参数]
    [U3D 添加大地、天空,用第一视角看看自己做的世界]
    [U3D 画起重机,绑脚本和控制它运动的基本操作]
    Flutter 国际化适配
    Error:Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Coul完美解决
  • 原文地址:https://www.cnblogs.com/Liang-jc/p/9155172.html
Copyright © 2020-2023  润新知