十三、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文件中每个字段都需要重写
  • 关联的数据是手动获取并循环展示在页面
相关推荐
BillKu几秒前
scss(sass)中 & 的使用说明
前端·sass·scss
疯狂的沙粒4 分钟前
uni-app 项目支持 vue 3.0 详解及版本升级方案?
前端·vue.js·uni-app
Jiaberrr14 分钟前
uniapp Vue2 获取电量的独家方法:绕过官方插件限制
前端·javascript·uni-app·plus·电量
点云SLAM15 分钟前
PyTorch 中contiguous函数使用详解和代码演示
人工智能·pytorch·python·3d深度学习·contiguous函数·张量内存布局优化·张量操作
尘浮72828 分钟前
60天python训练计划----day45
开发语言·python
哆啦A梦的口袋呀39 分钟前
基于Python学习《Head First设计模式》第六章 命令模式
python·学习·设计模式
努力搬砖的咸鱼41 分钟前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
Calvex44 分钟前
PyCharm集成Conda环境
python·pycharm·conda
谢尔登1 小时前
【React】React 18 并发特性
前端·react.js·前端框架
Joker`s smile1 小时前
使用React+ant Table 实现 表格无限循环滚动播放
前端·javascript·react.js