功能篇之登录注册

django中的登录注册

flask中的登录注册

一、django中的登录注册

#models.py文件中
from django.db import models
from multiselectfield import MultiSelectField
from django.utils.safestring import mark_safe
from rbac.models import User

class UserProfile(User,models.Model): """ 用户表 """ username = models.EmailField(max_length=255, unique=True, ) password = models.CharField(max_length=128, verbose_name='密码') name = models.CharField('名字', max_length=32) department = models.ForeignKey('Department', default=None, blank=True, null=True) mobile = models.CharField('手机', max_length=32, default=None, blank=True, null=True) memo = models.TextField('备注', blank=True, null=True, default=None) date_joined = models.DateTimeField(auto_now_add=True) is_active = models.BooleanField(default=True) def __str__(self): return self.name #继承的权限组件中的User类 class User(models.Model): """ 用户表 """ # name = models.CharField(max_length=32, verbose_name='用户名') # pwd = models.CharField(max_length=32, verbose_name='密码') roles = models.ManyToManyField(Role, blank=True) class Meta: abstract = True # 执行数据库迁移命令时不会生成具体的表,这张表做基类
#views视图中(auth)
from django.shortcuts import render, redirect, HttpResponse, reverse
from crm import models
from crm.forms import RegForm
import hashlib
from django.views.decorators.csrf import ensure_csrf_cookie
from rbac.service.permission import init_permission

def index(request):
    return HttpResponse('index')

# @ensure_csrf_cookie
def login(request):
    if request.method == 'POST':
        user = request.POST.get('username')
        pwd = request.POST.get('password')

        md5 = hashlib.md5()
        md5.update(pwd.encode('utf-8'))
        pwd = md5.hexdigest()

        obj = models.UserProfile.objects.filter(username=user, password=pwd, is_active=True).first()
        if obj:
            request.session['pk'] = obj.pk
            init_permission(request,obj)
            return redirect(reverse('customer_list'))

    return render(request, 'login.html')


def reg(request):
    form_obj = RegForm()
    if request.method == 'POST':
        form_obj = RegForm(request.POST)
        if form_obj.is_valid():
            # 插入到数据库
            # print(form_obj.cleaned_data)
            # form_obj.cleaned_data.pop('re_pwd')
            # models.UserProfile.objects.create(**form_obj.cleaned_data)
            form_obj.save()
            return redirect(reverse('login'))

    return render(request, 'reg.html', {'form_obj': form_obj})
#login.html中
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'css/reset.css' %}">
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <link rel="stylesheet" href="{% static 'css/supersized.css' %}">


</head>
<body oncontextmenu="return false">
<div class="page-container">
    <h1>Login</h1>
    <form action="" method="post">
        {% csrf_token %}
        <div>
            <input type="text" name="username" class="username" placeholder="Username" autocomplete="off">
        </div>
        <div>
            <input type="password" name="password" class="password" placeholder="Password" oncontextmenu="return false"
                   onpaste="return false">
        </div>
        <button id="submit" type="submit">Sign in</button>
    </form>
    <div class="connect">
        <p style="left: 0%;">If we can only encounter each other rather than stay with each other,then I wish we had
            never encountered.</p>
        <p style="margin-top: 20px; left: 0%;">如果只是遇见,不能停留,不如不遇见。</p>
    </div>
</div>
<div class="alert" style="display:none">
    <h2>消息</h2>
    <div class="alert_con">
        <p id="ts"></p>
        <p style="line-height:70px"><a class="btn">确定</a></p>
    </div>
</div>

<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="{% static 'js/supersized.3.2.7.min.js' %}"></script>
<script src="{% static 'js/supersized-init.js' %}"></script>
<script>
</script>
</body>
</html>
#reg.html中
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Title</title>
    <link rel="stylesheet" href="{% static 'plugins/bootstrap/css/bootstrap.css' %}">

</head>
<body>


<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2" style="margin-top: 90px">
            <form class="form-horizontal" action="" method="post" novalidate>
                {% csrf_token %}

                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.username.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.username }}
                        {{ form_obj.username.errors.0 }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.password.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.password }}
                        {{ form_obj.password.errors.0 }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.re_pwd.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.re_pwd }}
                        {{ form_obj.re_pwd.errors.0 }}
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.name.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.name }}
                        {{ form_obj.name.errors.0 }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.mobile.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.mobile }}
                        {{ form_obj.mobile.errors.0 }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">{{ form_obj.department.label }}</label>
                    <div class="col-sm-10">
                        {{ form_obj.department }}
                        {{ form_obj.department.errors.0 }}
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">Sign in</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


</body>
</html>

 

 

二、flask中的登录注册

 

posted @ 2019-06-06 11:49  海予心  阅读(186)  评论(0编辑  收藏  举报