day62 作业

熟练使用无名有名分组

urls.py

 url(r'^edit/(d+)/',views.edit_user,name='edit'),

views.py

def edit_user(request,edit_id):

    edit_obj = models.Author.objects.filter(id=edit_id).first()
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        # 第一种更新操作,全部更新
        models.Author.objects.filter(id=edit_id).update(username=username,password=password)
        # 第二种更新,拿到数据对象再更新,这种更新方式在字段多的时候会效率很慢
        edit_obj.username = username
        edit_obj.password = password
        edit_obj.save()

        return redirect('/userlist/')
    return render(request,'edit.html',locals())

userlist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-lg-8">
            <table class="table table-striped table-hover text-center">
                <thead>
                    <tr>
                        <th class="text-center">id</th>
                        <th class="text-center">username</th>
                        <th class="text-center">password</th>
                        <th class="text-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for obj in user_queryset %}
                        <tr>
                            <td>{{ obj.id }}</td>
                            <td>{{ obj.username }}</td>
                            <td>{{ obj.password }}</td>
                            <td>
                                <a href="{% url 'edit' obj.id %}" class="btn-xs btn-success">编辑</a>
                                <a href="/user_delete/?id={{ obj.id }}" class="btn-xs btn-danger">删除</a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>
原文地址:https://www.cnblogs.com/hz2lxt/p/12977055.html