用户管理

创建用户

使用django shell 创建普通用户:创建users最直接的方法是使用create_user()辅助函数

from django.contrib.auth.models import User
user= User.objects.create_user("rock","rock@51reboot.com","123456")

创建管理员

python manage.py createsuperuser --username=reboot --email=reboot@51reboot.com

批量添加用户

In [1]: from django.contrib.auth.models import User

In [2]: for n in range(1,100):
   ...:     username = "rock-{}".format(n)
   ...:     User.objects.create_user(username,"{}@51reboot.com".format(username),"123456")

User.objects.all()查询所有用户

视图

def user_list_view(request):
    user_queryset=User.objects.all()
    for user in user_queryset:
        print(user.username,user.email)
    return render(request,"user/userlist.html",{"userlist":user_queryset })

url

url(r'^user/list/$', views.user_list_view, name='user_list'),

模板

(env) [root@ES-10-1-21-55-B28 accounts]# cat templates/user/userlist.html 
{% extends "public/layout.html" %}
{% block body %}
<table class="table table-striped">
<tr>
    <th>序号</th>
    <th>用户名</th> 
    <th>email</th>
</tr>
    {% for user_obj in userlist %}
    <tr>
    <td>{{ forloop.counter }}</td>
    <td>{{ user_obj.username  }}</td>
    <td>{{ user_obj.email }}</td>
    </tr>
    {% endfor %}
</table>
{% endblock %}
原文地址:https://www.cnblogs.com/jiaqili/p/14606934.html