首页 > 数据库技术 > 详细

前后台交互实例二:前台通过django在数据库里面增删改查数据

时间:2018-04-30 10:05:10      阅读:457      评论:0      收藏:0      [点我收藏+]
 url(r‘^userinfo/‘, views.userinfo),
      url(r‘^userdetail-(?P<nid>\d+)/‘, views.userdetail),
      url(r‘^userdel-(?P<nid>\d+)/‘, views.userdel),
      url(r‘^useredit-(?P<nid>\d+)/‘, views.user_edit),

  views

def userinfo(request):
    if request.method =="GET":
        user_list=models.UserInfo.objects.all()
        return render(request,‘userinfo.html‘,{‘user_list‘:user_list})
    elif request.method == "POST":
        u = request.POST.get(‘user‘)
        p = request.POST.get(‘pwd‘)
        models.UserInfo.objects.create(username=u,password=p)
        return redirect(‘/CC/userinfo/‘)

def userdetail(request,nid):
    obj=models.UserInfo.objects.filter(id=nid).first()
    return render(request,‘userdetail.html‘,{‘obj‘:obj})

def userdel(request,nid):
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect(‘/CC/userinfo/‘)

def user_edit(request, nid):
    if request.method == "GET":
        obj = models.UserInfo.objects.filter(id=nid).first()
        return render(request, ‘user_edit.html‘,{‘obj‘: obj})
    elif request.method == "POST":
        nid = request.POST.get(‘id‘)
        u = request.POST.get(‘username‘)
        p = request.POST.get(‘password‘)
        models.UserInfo.objects.filter(id=nid).update(username=u,password=p)
        return redirect(‘/CC/userinfo/‘)

    model.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.
class UserInfo(models.Model):
    username=models.CharField(max_length=32)
    password=models.CharField(max_length=64)

class zte(models.Model):
    id=models.AutoField(primary_key=True)  #主键,自增
    username=models.CharField(max_length=32)
    password=models.CharField(max_length=64)
    email=models.CharField(max_length=64,null=True)

  

  userinfo.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white">
        ---------------------------------------
    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 200px;background-color: brown;">
            <a class="menu" href="/CC/userinfo/">用户管理</a>
            <a class="menu" href="/CC/usergroup/">用户组管理</a>
        </div>
        <div style="position:absolute;top:48px;left: 210px;bottom: 0;right: 0;overflow: auto">

            <h3>添加用户</h3>
            <form method="POST" action="/CC/userinfo/">
                <input type="text" name="user" />
                <input type="text" name="pwd" />
                <input type="submit" value="添加"/>
            </form>

            <h3>用户列表</h3>
            <ul>
                {% for row in user_list %}
                    <li>
                        <a href="/CC/userdetail-{{ row.id }}/">{{ row.username }}</a> |
                        <a href="/CC/userdel-{{ row.id }}/">删除</a> |
                        <a href="/CC/useredit-{{ row.id }}/">编辑</a>
                    </li>
                {% endfor %}
            </ul>


        </div>

    </div>

</body>
</html>

  useredit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .menu{
            display: block;
            padding: 5px;

        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white">
        11111111111111111
    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 200px;background-color: brown;">
            <a class="menu" href="/CC/userinfo/">用户管理</a>
            <a class="menu" href="/CC/usergroup/">用户组管理</a>
        </div>
        <div style="position:absolute;top:48px;left: 210px;bottom: 0;right: 0;overflow: auto">

            <h1>编辑用户</h1>
            <form method="post" action="/CC/useredit-{{ obj.id }}/">
                <input style="display: none" type="text" name="id" value="{{ obj.id }}" />
                <input type="text" name="username" value="{{ obj.username }}" />
                <input type="text" name="password" value="{{ obj.password }}"/>
                <input type="submit" value="提交" />
            </form>

        </div>

    </div>

</body>
</html>

  userdetail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .menu{
            display: block;
            padding: 5px;

        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white">
        3333333333333333333333333333333
    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 200px;background-color: brown;">
            <a class="menu" href="/CC/userinfo/">用户管理</a>
            <a class="menu" href="/CC/usergroup/">用户组管理</a>
        </div>
        <div style="position:absolute;top:48px;left: 210px;bottom: 0;right: 0;overflow: auto">

            <h1>用户详细信息</h1>

            <h5>{{ obj.id }}</h5>
            <h5>{{ obj.name }}</h5>
            <h5>{{ obj.password }}</h5>

        </div>

    </div>

</body>
</html>

  

  

前后台交互实例二:前台通过django在数据库里面增删改查数据

原文:https://www.cnblogs.com/qiangayz/p/8972717.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!