• django实例(1)


    Urls.py

    from django.contrib import admin
    from django.conf.urls import url


    from cmdb import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        # path('home.html/', views.home),
        url(r'^login/',views.login),
        url(r'^home',views.home),
    ]

    Cmdb-->Views.py

    from django.shortcuts import render

    # Create your views here.
    import time
    from django.shortcuts import HttpResponse
    from django.shortcuts import render
    from django.shortcuts import redirect
    # def home(request):
    #
    #     return HttpResponse('<h1>CMDB</h1>')

    def login(request):

        #包含用户提交的所有信息

        #获取用户提交方法
        # print(request.method)
        error_msg=""
        if request.method=="POST":
            # 用户通过post提交过来的数据
            user=request.POST.get('user',None)
            pwd=request.POST.get('pwd',None)
            if user=="root" and pwd=="123":
                # 去跳转到
                return redirect('/home')  #跳转 并重定向
            else:
                # 用户名密码不匹配
                error_msg="用户名或密码错误"
        return render(request,'login.html',{'error_msg':error_msg}) #找到模板并返回给用户
      # django替换'error_msg'

    user_list=[
        {'id':1, 'username':'haha','email':'asdsdd@126.com','gender':'男'},
        {'id':2, 'username':'wulei','email':'asdsdd@126.com','gender':'男'},
        {'id':3, 'username':'niuge','email':'asdsdd@126.com','gender':'男'},
    ]
    # for index in range(20):
    #     temp={'username':'haha'+str(index),'email':'asdsdd@126.com','gender':'男'}
    #     user_list.append(temp)

    def home(request):
        if request.method=="POST":
            # 获取用户提交的数据POST请求中
            u=request.POST.get('username')
            e=request.POST.get('email')
            g=request.POST.get('gender')
            temp = {'username': u, 'email':e, 'gender': g}
            user_list.append(temp)

        return render(request,'home.html',{'user_list':user_list})


    #主机管理
    #防火墙
    #。。。

    # def login(request):
    #     string = """
    #         <form>
    #             <input type='text'>
    #         </form>
    #         """
    #     f=open('templates/login.html','r',encoding='utf-8')
    #     data=f.read()
    #     f.close()
    #     return HttpResponse(data)

    Static-->Commons.css

    body{
        background-color: #eeeeee;
    }

    Templates-->login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/commons.css"/>
        <style>
            label{
                width:80px;
                text-align: right;
                display:inline-block ;
            }
        </style>
    </head>
    <body>
        <form action="/login/" method="post">
            <p>
                <label for="username">用户名:</label>
                <input id="username" name="user" type="text"/>
            </p>
            <p>
                <label for="pwd_1">密码:</label>
                <input id="pwd_1" name="pwd" type="password"/>
                <input type="submit" value="提交"/>
                <span style="color: red;">{{error_msg }}</span>
            </p>
        </form>
        <script src="/static/jquery-1.12.4.js"></script>
    </body>
    </html>

    Templates-->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>
                {%  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>
        <div>
            <form action="/del_host" method="post">
                <input style="display: none" id="nid" type="text" name="nid">
                <p>
                    <input type="submit"/>
                    <input type="button">
                </p>
            </form>
        </div>
        <script src="/static/jquery-1.12.4.js"></script>
        <script>
            $('.del').click(function () {
                var row_id=$(this).attr('row-id');
                $('#nid').val(row_id);
            })



        </script>
    </body>
    </html>

    Templates-->home_bak.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>
                {%  for row in user_list %}
                <tr>
                    <td>{{ row.username }}</td>
                    <td>{{ row.gender }}</td>
                    <td>{{ row.email }}</td>
                </tr>
               {% endfor %}
            </table>
        </div>

    </body>
    </html>

    Admin.py

    from django.contrib import admin
    from cmdb import models
    # Register your models here.
    admin.site.register(models.UserInfo)
    admin.site.register(models.UserType)

    Models.py

    from django.db import models

    # Create your models here.
    class UserType(models.Model):
        name=models.CharField(max_length=32)

    class UserInfo(models.Model):

        username=models.CharField(max_length=30)
        pwd=models.CharField(max_length=32)
        email=models.CharField(max_length=32)
        user_type=models.ForeignKey(UserType,on_delete=models.CASCADE,)

    创建超级用户 python manage.py createsuperuser  xxx 

  • 相关阅读:
    MianShiTi
    C#多线程解决界面卡死问题的完美解决方案
    C#中的WebBrowser控件的使用
    解决Linq第一次调用存储过程时速度慢的问题
    AX 的 NumberSequences
    Ax 从一个form关闭另外一个form,AX全局变量
    查询数据库后台Block 的Sql存储过程
    Dynamic AX 4.0 用户组权限SecurityKey
    20160408 从软件工程的3大文档开始说起
    获取AX的窗口所有控件的lableID及内容
  • 原文地址:https://www.cnblogs.com/leiwenbin627/p/10981024.html
Copyright © 2020-2023  润新知