zoukankan      html  css  js  c++  java
  • 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>
        
        
        
  • 相关阅读:
    在PictureBox中打开图片文件
    while和do-while语句的异同之处
    if选择语句与switch选择语句的比较、区别及应用场景
    二进制数的原码 反码 补码的作用及转换
    一篇文章看懂二进制、八进制、十进制、十六进制数据转换
    ASCII,unicode, utf8 ,big5 ,gb2312,gbk,gb18030等几种常用编码区别(转载)
    百钱买百鸡问题
    鸡兔同笼问题
    用for循环打印九九乘法表(for嵌套循环)
    用*号打印直角三角形(正反)
  • 原文地址:https://www.cnblogs.com/bianzhuo/p/9948453.html
Copyright © 2011-2022 走看看