十三、Django之添加用户(原始方法实现)

修改urls.py

复制代码
    path("user/add/", views.user_add),

添加user_add.html

复制代码
{% extends 'layout.html' %}
{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">新建用户</h3>
            </div>
            <div class="panel-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label>姓名</label>
                        <input type="text" class="form-control" placeholder="姓名" name="name"/>
                    </div>
                    <div class="form-group">
                        <label>密码</label>
                        <input type="text" class="form-control" placeholder="密码" name="pwd"/>
                    </div>
                    <div class="form-group">
                        <label>年龄</label>
                        <input type="text" class="form-control" placeholder="年龄" name="age"/>
                    </div>
                    <div class="form-group">
                        <label>余额</label>
                        <input type="text" class="form-control" placeholder="余额" name="money"/>
                    </div>
                    <div class="form-group">
                        <label>入职时间</label>
                        <input type="text" class="form-control" placeholder="入职时间" name="time"/>
                    </div>
                    <div class="form-group">
                        <label>性别</label>
{#                        可以这么写,但是如果下拉选项过多的时候很费劲#}
{#                        <select class="form-control">#}
{#                            <option value="1">男</option>#}
{#                            <option value="2">女</option>#}
{#                        </select>#}
                        <select class="form-control" name="gender">
                            {% for item in gender_choices %}
                            <option value="{{ item.0 }}">{{ item.1 }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="form-group">
                        <label>部门</label>
                        <select class="form-control" name="dept">
                            {% for item in depart_list %}
                                <option value="{{ item.id }}">{{ item.title }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <button type="submit" class="btn btn-primary">提 交</button>
                </form>
            </div>
        </div>
    </div>
{% endblock %}

修改view.py

复制代码
def user_add(request):
    if request.method=="GET":
        context = {
            "gender_choices": models.UserInfo.gender_choices,
            "depart_list": models.Department.objects.all()
        }
        return render(request, "user_add.html", context)

    # 获取用户提交的数据
    name = request.POST.get("name")
    pwd = request.POST.get("pwd")
    age = request.POST.get("age")
    money = request.POST.get("money")
    time = request.POST.get("time")
    gender = request.POST.get("gender")
    dept = request.POST.get("dept")

#     添加数据到数据库中
    models.UserInfo.objects.create(name=name, password=pwd, age=age, account=money, 
                                   create_time=time, gender=gender, depart_id=dept)
    return redirect("/user/list/")

修改user_list.html

注意将user_list的路由替换

复制代码
            <a class="btn btn-success" href="/user/add">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                新建用户
            </a>

页面测试

缺点

  • 用户提交数据没有校验
  • 用户输错信息页面上没有错误提示
  • html文件中每个字段都需要重写
  • 关联的数据是手动获取并循环展示在页面
相关推荐
ywf12151 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
程序员爱钓鱼1 小时前
Go排序核心库: sort包深度指南
后端·面试·go
2501_945423541 小时前
用Matplotlib绘制专业图表:从基础到高级
jvm·数据库·python
2301_793804691 小时前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python
恋猫de小郭1 小时前
2026,Android Compose 终于支持 Hot Reload 了,但是收费
android·前端·flutter
hpoenixf7 小时前
2026 年前端面试问什么
前端·面试
还是大剑师兰特7 小时前
Vue3 中的 defineExpose 完全指南
前端·javascript·vue.js
吴佳浩7 小时前
GPU 编号进阶:CUDA\_VISIBLE\_DEVICES、多进程与容器化陷阱
人工智能·pytorch·python
泯泷7 小时前
阶段一:从 0 看懂 JSVMP 架构,先在脑子里搭出一台最小 JSVM
前端·javascript·架构
大阿明7 小时前
Spring Boot(快速上手)
java·spring boot·后端