zoukankan      html  css  js  c++  java
  • 用户管理

    创建用户

    使用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 %}
  • 相关阅读:
    C++ vector介绍
    C++string的使用
    关于VS2010error RC2170 : bitmap file res mp1.bmp is not in 3.00 format
    团队项目第一次讨论
    团队项目——铁大树洞个人分析
    第五周学习进度总结
    转发
    android移动端疫情展示
    《构建之法》阅读笔记03
    第四周学习进度总结
  • 原文地址:https://www.cnblogs.com/jiaqili/p/14606934.html
Copyright © 2011-2022 走看看