第16 17章节-Python3.5-Django知识点整理 15

简介: 知识点整理:内容整理 1. 创建Django工程 django-admin startproject 工程名 2.

知识点整理:


内容整理
    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>
    
    
    
XXOO管理:
    MySQL
    SQLAlchemy
    主机管理(8列):
        IP
        端口
        业务线
        ...
        
    用户表:
        用户名
        密码
    
    功能:
        1、 登录
        2、主机管理页面
            - 查看所有的主机信息(4列)
            - 增加主机信息(8列) ** 模态对话框
        3、查看详细
            url:
                "detail" -> detail
        
            def detail(reqeust):
                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')
            
    


模态对话框

修改 home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="margin: 0">
    <div style="height: 48px;background-color: #dddddd"></div>
    <div>
        <form action="/home" method="post">
            <input type="text" name="username" placeholder="用户名">
            <input type="text" name="email" placeholder="邮箱">
            <input type="text" name="gender" placeholder="性别">
            <input type="submit" value="添加">
        </form>
    </div>
    <div>
        <table>
            <!--django支持 for 循环,与Python不一样,这是模板语言for循环 有开头就有结尾 endfor-->
            {% for row in user_list %}
                 <tr>
                    <td>{{ row.username }}</td>
                    <td>{{ row.gender }}</td>
                    <td>{{ row.email }}</td>
                    <td>
                        <a href="/detail?nid={{ row.id }}">查看详细</a>
                        <a class="del" href="#" row-id="{{ row.id }}">删除</a>
                    </td>
                </tr>
            {% endfor %}

        </table>
    </div>
    <!--绑定事件-->
    $('.del').click(function(){
        var row_id = $(this).attr('row-id');
        $('#nid').val(row_id);
    })
    <div>
        <form action="/del_host" method="post">
            <input style="display: none" id="nid" type="text" name="nid">
            <p>
                <input type="submit">
                <input type="botton">
            </p>
        </form>
    </div>

</body>
</html>
image.png

image.png
  • 效果图:
image.png
目录
相关文章
|
3月前
|
存储 搜索推荐 数据库
关于“Python”的核心知识点整理大全58
关于“Python”的核心知识点整理大全58
32 2
|
1月前
|
存储 数据可视化 数据挖掘
Python的知识点非常广泛
Python的知识点非常广泛
12 0
|
1月前
|
存储 Java C++
【python基础题】——知识点选择、填空、简答
【python基础题】——知识点选择、填空、简答
34 0
|
2月前
|
Java Shell 索引
[Python]知识点
[Python]知识点
76 0
[Python]知识点
|
3月前
|
存储 开发工具 文件存储
Python的核心知识点整理大全66(已完结撒花)
Python的核心知识点整理大全66(已完结撒花)
78 4
|
3月前
|
Linux 开发工具 git
关于“Python”的核心知识点整理大全65
关于“Python”的核心知识点整理大全65
23 1
关于“Python”的核心知识点整理大全65
|
3月前
|
安全 开发工具 数据库
关于“Python”的核心知识点整理大全64
关于“Python”的核心知识点整理大全64
29 0
关于“Python”的核心知识点整理大全64
|
3月前
|
Shell 开发工具 数据库
关于“Python”的核心知识点整理大全63
关于“Python”的核心知识点整理大全63
24 3
关于“Python”的核心知识点整理大全63
|
3月前
|
存储 关系型数据库 数据库
关于“Python”的核心知识点整理大全62
关于“Python”的核心知识点整理大全62
30 4
关于“Python”的核心知识点整理大全62
|
3月前
|
前端开发 Linux Python
关于“Python”的核心知识点整理大全61
关于“Python”的核心知识点整理大全61
29 0
关于“Python”的核心知识点整理大全61

热门文章

最新文章