zoukankan      html  css  js  c++  java
  • 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    
  • 相关阅读:
    bootstrap-select.js 下拉框多选后动态赋值
    vs2012 未找到与约束 ContractName Microsoft.VisualStudio.Utilities.IContentTy...
    jquery 报错 Uncaught TypeError: Illegal invocation
    火狐浏览器的RestClient,接口测试,Post提交数据
    destoon二次开发 操作数据库可运行示例
    ZendStudio13 PHP调试环境快速配置
    VR发展的最大障碍在于内容?
    优秀博文链接
    LoopMatrix
    串口输出float型数据
  • 原文地址:https://www.cnblogs.com/Liang-jc/p/9155172.html
Copyright © 2011-2022 走看看