• django 第二天


     进行了前后端简单的链接

    view 视图代码如下

    from django.shortcuts import render
    from  django.http import HttpResponse
    from django.shortcuts import redirect
    
    def s1(request):
        return HttpResponse('<h1>hello<h1>')
    
    def login(request):
        error_msg=""
        if request.method=="POST":
            user=request.POST.get('user',None)
            pwd=request.POST.get('pwd',None)
            if user=="root" and pwd=="123":
                return  redirect("http://www.baidu.com")#实现重定向,如果网址写错的话,会自动加到链接的后面
            else:
                error_msg="用户名或密码错误"
    
        return render(request ,'login.html',{'error_msg':error_msg})
    
    # Create your views here.

    login页面代码视图如下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/common.css">
    </head>
    <body>
        <form action="/login/" method="post">
            <p>
                <label for="username">用户名</label>
                <input id="username" name="user" type="text">
            </p>
               <p>
                <label for="password">密码</label>
                <input id="password" name="pwd" type="text">{{error_msg}}
                   <input type="submit" value="提交">
            </p>
        </form>
    </body>
    </html>
    View Code

    账号必须是root 密码必须是123 才能够重定向 否则 就会这样

    如果符合条件就会跳转到百度


    注意细节

    post的话,action方法必须/login/ 否则会报错 或者修改setting的文件


    又新建了home页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="/home/" method="post">
            <input type="text" name="username" placeholder="用户名">
            <input type="text" name="password" placeholder="密码">
            <input type="text" name="sex" placeholder="性别">
            <input type="submit" value="添加">
        </form>
        <table>
        {% for row in userlist %}
             <tr>
                <td>{{row.username}}</td>
                <td>{{row.password }}</td>
                <td>{{row.sex}}</td>
             </tr>
        {% endfor %}<!-必须加endfor-!>这是模板语言
        </table>
    </body>
    </html>

    view 的视图函数如下

    def home(request):
        if request.method=="POST":
            u=request.POST.get('username',None)
            p=request.POST.get('password',None)
            s=request.POST.get('sex',None)
            temp={"username": u,"password": p,"sex":s}
    
            userlist.append(temp)
        return  render(request,'home.html',{"userlist":userlist})#userlist传递给前端页面
    

      

    内容整理

    内容整理
        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>
        
        
        
    View Code

    内容整理
        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>
        
        
        
  • 相关阅读:
    Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法
    JAVA_用Java来获取访问者真实的IP地址
    Html5_移动前端不得不了解的html5 head 头标签
    ThinkPad_T430重装系统
    JavaScript_JS判断客户端是否是iOS或者Android
    Html5_禁止Html5在手机上屏幕页面缩放
    HttpClient_httpclient 4.3.1 post get的工具类
    HttpClient_使用httpclient必须知道的参数设置及代码写法、存在的风险
    LATEX数学公式基本语法
    为WLW开发Latex公式插件
  • 原文地址:https://www.cnblogs.com/bianzhuo/p/9948453.html
Copyright © 2020-2023  润新知