Django:第一个项目

用户管理系统:

1.创建数据库

create database gx_day15 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

2.修改Django项目Setting.py文件配置:

 

 3.更改初始页面

将初始页面更改为app的页面

path(r' ',views.welcome) ,当引号内为空时,url将变为网站的域名:127.0.0.1:8000。

程序收到用户请求时,会先从根目录的urls.py中寻找该URL属于哪个APP,再从APP的urls.py中寻找对应的URL,并执行其view函数。

4.编写views.welcome

运行Django项目

 5.编写login页面

#  在app/urls.py中编写login的URL信息

path(r'login/', views.login),

 

#  在app/views.py下编写login的视图函数

def login(request):
    if request.method == "GET":
        return render(request,"login.html")
    else:
        #  如果是POST请求,那么返回用户提交的数据
        # print(request.POST)
        username = request.POST.get("user")
        password = request.POST.get("pwd")

        if username == "admin" and password == "tb123":
            # return HttpResponse("登录成功")
            return HttpResponseRedirect("https://www.baidu.com/")
        else:
            return render(request,"login.html",{"tips": "登录失败,username/password错误"})
#在app/templates下编写login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="POST" action="/login/">
    {% csrf_token %}
    <input type="text" name="user" placeholder="用户名">
    <input type="password" name="pwd" placeholder="密码">
    <input type="submit" value="提交">
    <span style="color: red;">{{ tips }}</span>
</form>
</body>
</html>

6.编写info_list页面

#  在app/urls.py中编写info_list的URL信息

path(r'info_list/', views.info_list),
#  在app/views.py下编写info_list的视图函数

def info_list(request):
    #  获取数据库中所有的用户信息
    data_list = UserInfo.objects.all()  #[对象,对象,对象]

    return render(request,"info_list.html",{"data_list":data_list})
#  编写info_list.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Info列表</h1>
    <a href="/info_add/">添加</a>
    <table border="1">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Password</th>
            <th>Age</th>
            <th>Sex</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for item in data_list%}
        <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.password }}</td>
            <td>{{ item.age }}</td>
            <td>{{ item.sex }}</td>
            <td>
                <a href="/depart_delete/?uid={{ item.id }}">删除</a>
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    </body>
    </html>

7.编写info_add页面

#  在app/urls.py中编写info_add的URL信息

path(r'info_add/', views.info_add),

 在app/下创建models.py文件,为数据库创建表,并在终端中执行命令:

1.python manage.py makemigrations

2.python manage.py migrate

 

#  在app/views.py下编写info_add的视图函数


def info_add(request):
    if request.method == "GET":
        return render(request,"info_add.html")
    #  获取用户提交的数据
    user = request.POST.get("user")
    password = request.POST.get("password")
    age = request.POST.get("age")
    sex = request.POST.get("sex")

    #  将提交的数据添加到数据库
    UserInfo.objects.create(name = user,password = password,age = age,sex = sex)
    # return HttpResponseRedirect("http://127.0.0.1:8000/info_list/")
    return HttpResponseRedirect("/info_list/")
#在app/templates下编写info_add.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>添加用户</h1>
<form method="post" action="/info_add/">
    {% csrf_token %}
    <input type="text" name="user" placeholder="用户名">
    <input type="text" name="password" placeholder="密码">
    <input type="text" name="age" placeholder="年龄">
    <input type="text" name="sex" placeholder="性别">
    <input type="submit" value="提交">
</form>
</body>
</html>

8.编写info_delete

#  在app/urls.py中编写info_delete的URL信息

path(r'info_delete/', views.info_delete),
#  在app/views.py下编写info_delete的视图函数

def info_delete(request):
    uid = request.GET.get("uid")
    UserInfo.objects.filter(id = uid).delete()
    return HttpResponseRedirect("/info_list/")
    #  重定向返回info_list页面

效果图:

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值