django Example (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 the render

# here Wallpaper the Create your views. Import Time from django.shortcuts Import the HttpResponse from django.shortcuts Import the render from django.shortcuts Import the redirect # DEF Home (Request): # # return the HttpResponse ( '<h1 of> the CMDB < / h1 of> ') DEF Login (Request): # contains all the information the user submits     # submission method to obtain the user     # Print (request.method) ERROR_MSG = "" IF request.method == "the pOST" : # user submits over by post data user = request.POST.get (










    



    
    
        
        'User' , None )
        pwd = request.POST.get ( 'pwd' , None )
        IF User == "the root" and pwd == "123" :
            # to jump to return the redirect ( '/ Home' )   # jumping turn and redirect the else : # username password do not match ERROR_MSG = "user name or password is incorrect" return the render (Request, 'the login.html' , { 'ERROR_MSG' : ERROR_MSG}) # template is found and returned to the user   # django replacement ' ERROR_MSG ' user_list = [     { ' ID ' :. 1, ' username ' :'ha ha' ,
            
        
            
            
    



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

DEF Home (Request):
    IF request.method == "the POST" :
        data acquisition POST # requests submitted by the user U = request.POST.get ( 'username' )         E = request.POST.get ( 'In Email' )         G request.POST.get = ( 'Gender' )         TEMP = { 'username' : U, 'In Email' : E, 'Gender' : G}         user_list.append (TEMP) return the render (Request, 'home.html' , { 'user_list' : user_list}) # host management # firewall #. . . The Login DEF # (Request): # String = "" "
        





    









#             <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>
            <: </Password>"pwd_1"for =labellabel>
            <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>
                <for example,>
                    <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,)

 

Guess you like

Origin www.cnblogs.com/leiwenbin627/p/10981024.html