首页 > 其他 > 详细

用户管理

时间:2021-04-01 19:06:04      阅读:14      评论:0      收藏:0      [点我收藏+]

创建用户

使用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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!