首页 > 代码库 > python 保障系统(一)

python 保障系统(一)

python  保障系统

from django.shortcuts import render,redirect,HttpResponse
from app01 import models
from django.forms import Form
from django.forms import fields
from utils.pager import PageInfo
# Create your views here.

class Logins(Form):
    # 定一个Form组件验证
    user=fields.CharField(

        min_length=3,
        strip=True,
        required=True,
        error_messages={
            ‘min_length‘:‘太短了‘,
            ‘required‘:‘不能为空‘,
        }
    )
    pwd=fields.CharField(
        min_length=5,
        strip=True,
        required=True,
        error_messages={
            ‘min_length‘: ‘太短了‘,
            ‘required‘: ‘不能为空‘,
        }
    )

定义一个FORM验证

url视图函数

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^login/‘, views.login),
    url(r‘^user_out/‘, views.user_out),
    url(r‘^registered/‘, views.registered),
    url(r‘^check_code/‘, views.check_code),

    url(r‘^Avatars/‘, views.Avatars),
    url(r‘^bokes/(\w+).html$‘, views.bokes),
    url(r‘^boke/(?P<type_id>\d+)/‘, views.boke),

    url(r‘^‘, views.boke),

]
技术分享
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
#导入生成随机字符的模块
def rd_check_code(width=120,height=30,char_length=5,font_file=kumo.ttf,font_size=28):
    # 定义一个生成验证码的函数,宽120,高30是验证码的背景图大小,char_langth是在这个背景图上能写几个字符,file是设置显示在背景的字体样式,font_size是字体大小
    code=[]
    # 定义一个空列表
    img=Image.new(mode=RGB,size=(width,height),color=(255,255,255))
    #设置一个img背景图颜色是使用RGB,背景图的大小是120,30 颜色是用RGB的255,255,255
    draw=ImageDraw.Draw(img,mode=RGB)
    #创建一个画板,画板背景图是img,颜色采用RGB
    def rndChar():
        ‘‘‘
        生成随机字母
        :return: 
        ‘‘‘
        return chr(random.randint(65,90))
        #返回一个转成字符的字母(65,90之间在ascii码表是26个字母的大写)
    def rndColor():
        ‘‘‘
        生成随机颜色
        :return: 
        ‘‘‘
        return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
        #返回一个组数字(由在ascii码表0,255对应的内容生成三个数字拼接)


    font=ImageFont.truetype(font_file,font_size)
    # 创建一生成个文本的(font_file是文本字体的样式,font_size是文本字体的大小)
    for i in range(char_length):
        ‘‘‘
        生成字符(文字)
        ‘‘‘
        # 循环在这个文本上最大能写几个字符总数
        char=rndChar()
        # 获取一个随机的字符
        code.append(char)
        # 追加到code列表中
        h=random.randint(0,4)
        # 从0之4之间生成随机的数字
        draw.text([i * width / char_length , h], char,font=font,fill=rndColor())
        #写文本(坐标1是每次循环的i数值乘以宽度的值在除以最大显示数值,h是从0之4生成的随机数字,组成---->0,h ---->24,h---->48,h---->120,h)
        #设置font=font是默认自己选择的字体类型,fill是填充的颜色是执行随机颜色函数获取的随机颜色组

    for i in range(5):
        ‘‘‘
        生成干扰点
        ‘‘‘
        draw.point([random.randint(0,width),random.randint(0,height)],fill=rndColor())
        #画点坐标1是0和,120之间随机生成的数值,坐标2是0和30随机生成的数值,填充的颜色是执行随机颜色函数获取的随机颜色组

    for i in range(5):
        ‘‘‘
         生成干扰圆圈
        ‘‘‘
        draw.point([random.randint(0,width),random.randint(0,height)],fill=rndColor())
        #画点坐标1是0和,120之间随机生成的数值,坐标2是0和30随机生成的数值,填充的颜色是执行随机颜色函数获取的随机颜色组
        x=random.randint(0,width)
        # x=0到120之间随机生成的数字
        y=random.randint(0,height)
        # y=0到30之间随机生成的数字
        draw.arc((x,y,x+4,y+4),0,90,fill=rndColor())
        #设置圆存在的坐标和圆的坐标,设置角度的起始位置,设置角度的结束位置,填充的颜色是执行随机颜色函数获取的随机颜色组
    for i in range(5):
        ‘‘‘
        设置干扰的线
        ‘‘‘
        x1=random.randint(0,width)
        # x1=从0到120之间生成随机的数字
        y1=random.randint(0,height)
        # y1=从0到30之间生成随机的数字
        x2 = random.randint(0, width)
        # x2 = 从0到120之间生成随机的数字
        y2 = random.randint(0, height)
        # y2=从0到30之间生成随机的数字
        draw.line((x1,y1,x2,y2),fill=rndColor())
        #设置画线的起始坐标和结束坐标,填充的颜色是执行随机颜色函数获取的随机颜色组
    img=img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    # 设置滤镜,让验证码的色差更加明显
    return img,‘‘.join(code)
    #返回img整个画板,和拼接的随机字符(字符串类型)
封装的验证码
def check_code(request):
    from io import BytesIO
    from app01.check.check_code import rd_check_code
    img,code=rd_check_code()
    stream=BytesIO()
    img.save(stream,‘png‘)
    request.session[‘yzm‘]=code
    return HttpResponse(stream.getvalue())

验证调用函数

技术分享
def login(request):
    if request.method==GET:
        return render(request,login.html)
    else:
        input_yzm=request.POST.get(yzm)
        session_yzm=request.session.get(yzm)
        if input_yzm.upper()==session_yzm.upper():
            obj = Logins(request.POST)
            if obj.is_valid():
                users=models.UserInfo.objects.filter(username=obj.cleaned_data[user],password=obj.cleaned_data[pwd]).first()
                if users:
                    userss = models.UserInfo.objects.filter(username=obj.cleaned_data[user], password=obj.cleaned_data[pwd]).values(
                        avatar,
                        username,
                        blog__article__title,
                        blog__article__comment,
                        blog__article__summary,
                        blog__article__comment_count,
                        blog__article__up_count,
                        blog__site,
                    )
                    print(userss[0][avatar])
                    request.session[users]={users:obj.cleaned_data[user],touxiang:userss[0][avatar],site:userss[0][blog__site]}
                    article_list = models.UserInfo.objects.filter(username=obj.cleaned_data[user]).count()
                    page_info = PageInfo(request.GET.get(page), article_list, 10, /home.html, 11)
                    # print(page_info.start())
                    article_list = models.UserInfo.objects.filter(username=obj.cleaned_data[user])[page_info.start():page_info.end()]
                    return redirect(/boke/)
                    # return render(request,‘boke.html‘,{
                    #     ‘touxiang‘:userss[0][‘avatar‘],
                    #     ‘a_list‘:userss,
                    #     ‘article_list‘: article_list,
                    #     ‘page_info‘:page_info,
                    #     ‘zx‘:‘注销‘,
                    #     ‘wdbok‘:‘我的博客‘,
                    # })
                return render(request, login.html, {zhm: 账号或密码错误!})
            return render(request, login.html, {obj:obj })
        else:
            return render(request,login.html,{msg:验证码错误!})
用户登录 views
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
    <style>
        .login{
            width: 600px;
            margin: 0 auto;
            padding: 20px;
            margin-top: 90px;
        }
    </style>
</head>
<body>
<div class="login">
    <form class="form-horizontal" action="/login/" method="POST">
      {% csrf_token %}
      <div class="form-group">
        <label  class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
          <input type="text" class="form-control"  placeholder="用户名" name="user">{{ obj.errors.user.0 }}
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
          <input type="password" class="form-control"  placeholder="密码" name="pwd">{{ obj.errors.pwd.0 }}
        </div>
      </div>
        <div class="form-group">
        <label class="col-sm-2 control-label">验证码</label>
        <div class="col-sm-5">
          <input type="text" class="form-control"  placeholder="验证码" name="yzm">
            {{ msg }}

        </div>
        <div class="col-sm-5">
          <img style="width: 120px;height: 30px;" src=http://www.mamicode.com/"/check_code/">
            &nbsp,&nbsp;
          <a href=http://www.mamicode.com/"/login/">更换验证码</a>
        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <p><input type="checkbox" value=http://www.mamicode.com/"一个月免登录" name="jj">一个月免登录</p>
          <input type="submit" class="btn btn-default" value=http://www.mamicode.com/"登录" />{{ zhm }}
        </div>

      </div>
    </form>
</div>
</body>
</html>
用户登录页面.html
技术分享
def boke(request,*args, **kwargs):
    # 获取当前URL
    print(request.path_info)
    condition = {}
    a_list=models.Article.objects.all()
    type_id = int(kwargs.get(type_id)) if kwargs.get(type_id) else None
    if type_id:
        a_list=models.Article.objects.filter(category=type_id)
        condition[article_type_id] = type_id
    type_choice_list = models.Article.type_choices
    article_list = models.Article.objects.filter(**condition).count()
    page_info = PageInfo(request.GET.get(page), article_list, 10, /home.html, 11)
    # print(page_info.start())
    article_list = models.Article.objects.filter(**condition)[page_info.start():page_info.end()]

    return render(
        request,
        boke.html,
        {
            a_list:a_list,
            type_choice_list: type_choice_list,
            article_list: article_list,
            page_info:page_info,
            type_id: type_id,
            touxiang:/static/8.png,
            users:登录,
            zx:注册,
            centen:测试

        }
    )
用户主页面 views
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" />
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/font-awesome-4.7.0/css/font-awesome.css" />
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/css/commons.css"/>
    <style>
        .touxiang{
        width:48px;
        height: 48px;
        border-radius: 50%;
}
    </style>
</head>
<body>
{#<nav class="navbar navbar-default no-radius">#}
{#  <div class="container">#}
{#    <!-- Brand and toggle get grouped for better mobile display -->#}
{#    <div class="navbar-header">#}
{#      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">#}
{#        <span class="sr-only">Toggle navigation</span>#}
{#        <span class="icon-bar"></span>#}
{#        <span class="icon-bar"></span>#}
{#        <span class="icon-bar"></span>#}
{#      </button>#}
{#      <a class="navbar-brand a1" href="http://www.mamicode.com/#">老子的技术论坛</a>#}
{#    </div>#}
{##}
{#    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">#}
{#        <ul class="nav navbar-nav">#}
{#            {% if type_id %}#}
{#                <li><a class="a1" href="http://www.mamicode.com/">全部</a></li>#}
{#            {% else %}#}
{#                <li><a class="active" href="http://www.mamicode.com/">全部</a></li>#}
{#            {% endif %}#}
{#            {% for item in type_choice_list %}#}
{#                {% if item.0 == type_id %}#}
{#                    <li class="active"><a class="a1" href="http://www.mamicode.com/boke/{{ item.0 }}/">{{ item.1 }}</a></li>#}
{#                {% else %}#}
{#                    <li><a class="a1" href="http://www.mamicode.com/boke/{{ item.0 }}/">{{ item.1 }}</a></li>#}
{#                {% endif %}#}
{#            {% endfor %}#}
{#        </ul>#}
{#    {% if request.session.users %}#}
{#      <ul class="nav navbar-nav navbar-right">#}
{#        <li><a class="a1" href="http://www.mamicode.com/bokes/{{ request.session.users.site }}.html">我的博客</a></li>#}
{#        <li><a class="a1" href="http://www.mamicode.com/#">{{ request.session.users.users }}</a></li>#}
{#        <li><a class="a1" href="http://www.mamicode.com/user_out/">注销</a></li>#}
{#        <li><img class="touxiang" src="http://www.mamicode.com/{{ request.session.users.touxiang }}"></li>#}
{#      </ul>#}
{#    {% else %}#}
{#        <ul class="nav navbar-nav navbar-right">#}
{#        <li><a class="a1" href="http://www.mamicode.com/login/"> 登录</a></li>#}
{#        <li><a class="a1" href="http://www.mamicode.com/registered/">注册</a></li>#}
{#        <li><img class="touxiang" src="http://www.mamicode.com/{{ touxiang }}"></li>#}
{#      </ul>#}
{#    {% endif %}#}
{#    </div>#}
{#  </div>#}
{#</nav>#}
{#{% if centen == ‘测试‘ %}#}
    {% if type_id %}
        <nav class="navbar navbar-default no-radius">
            <div class="container">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                            data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand a1" href=http://www.mamicode.com/"#">老子的技术论坛</a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        {% if type_id %}
                            <li><a class="a1" href=http://www.mamicode.com/"/">全部</a></li>
                        {% else %}
                            <li class="active"><a href=http://www.mamicode.com/"/">全部</a></li>
                        {% endif %}
                        {% for item in type_choice_list %}
                            {% if item.0 == type_id %}
                                <li class="active"><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                            {% else %}
                                <li><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                            {% endif %}
                        {% endfor %}
                    </ul>
                    {% if request.session.users %}
                        <ul class="nav navbar-nav navbar-right">
                            <li><a class="a1" href=http://www.mamicode.com/"/bokes/{{ request.session.users.site }}.html">我的博客</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"#">{{ request.session.users.users }}</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"/user_out/">注销</a></li>
                            <li><img class="touxiang" src=http://www.mamicode.com/"{{ request.session.users.touxiang }}"></li>
                        </ul>
                    {% else %}
                        <ul class="nav navbar-nav navbar-right">
                            <li><a class="a1" href=http://www.mamicode.com/"/login/"> 登录</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"/registered/">注册</a></li>
                            <li><img class="touxiang" src=http://www.mamicode.com/"{{ touxiang }}"></li>
                        </ul>
                    {% endif %}
                </div>
            </div>
        </nav>
        <div>
            <div class="container">
                <div class="col-md-8">
                    <div class="article-list">
                        <div class="article-item">
                            {% for row  in a_list %}
                                <div><a class="a1">{{ row.title }}</a></div>
                                <div class="art-body clearfix">
                                    <a><img class="left a1" src=http://www.mamicode.com/"{{ row.blog.user.avatar }}"></a>
                                    <a class="a1">{{ row.summary }}</a>
                                </div>
                                <a class="a1">
                                    <p><i class="fa fa-user-o" aria-hidden="true"></i>
                                        {{ row.blog.user.username }} 发布于 2017-07-10
                                        <i class="fa fa-commenting-o" aria-hidden="true"></i>
                                        {{ row.comment_count }}
                                        <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>
                                        {{ row.up_count }}
                                    </p>

                                    <hr style="border:0.5px dotted darkgray"/>
                                </a>
                            {% endfor %}
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    {{ page_info.pager|safe }}
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-heading">Panel heading without title</div>
                        <div class="panel-body">
                            <p>人生若只如初见,何事秋风悲画扇</p>
                            <p>等闲变却故人心,却道故人心易变</p>
                            <p>骊山语罢清宵半,泪雨霖铃终不怨</p>
                            <p>何如薄幸锦衣郎,比翼连枝当日愿</p>
                        </div>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">Panel heading without title</div>
                        <div class="panel-body">
                            <p>十年生死两茫茫,不思量,自难忘</p>
                            <p>千里孤坟,无处话凄凉</p>
                            <p>纵使相逢应不识,尘满面,鬓如霜</p>
                            <p>夜来幽梦忽还乡,小轩窗,正梳妆</p>
                            <p>相顾无言,惟有泪千行</p>
                            <p>料得年年肠断处,明月夜,短松冈</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {% else %}
        <nav class="navbar navbar-default no-radius">
            <div class="container">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                            data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand a1" href=http://www.mamicode.com/"#">老子的技术论坛</a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        {% if type_id %}
                            <li><a class="a1" href=http://www.mamicode.com/"/">全部</a></li>
                        {% else %}
                            <li class="active"><a href=http://www.mamicode.com/"/">全部</a></li>
                        {% endif %}
                        {% for item in type_choice_list %}
                            {% if item.0 == type_id %}
                                <li class="active"><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                            {% else %}
                                <li><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                            {% endif %}
                        {% endfor %}
                    </ul>
                    {% if request.session.users %}
                        <ul class="nav navbar-nav navbar-right">
                            <li><a class="a1" href=http://www.mamicode.com/"/bokes/{{ request.session.users.site }}.html">我的博客</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"#">{{ request.session.users.users }}</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"/user_out/">注销</a></li>
                            <li><img class="touxiang" src=http://www.mamicode.com/"{{ request.session.users.touxiang }}"></li>
                        </ul>
                    {% else %}
                        <ul class="nav navbar-nav navbar-right">
                            <li><a class="a1" href=http://www.mamicode.com/"/login/"> 登录</a></li>
                            <li><a class="a1" href=http://www.mamicode.com/"/registered/">注册</a></li>
                            <li><img class="touxiang" src=http://www.mamicode.com/"{{ touxiang }}"></li>
                        </ul>
                    {% endif %}
                </div>
            </div>
        </nav>
        <div>
            <div class="container">
                <div class="col-md-8">
                    <div class="article-list">
                        <div class="article-item">
                            {% for row  in a_list %}
                                <div><a class="a1">{{ row.title }}</a></div>
                                <div class="art-body clearfix">
                                    <a class="a1"><img class="left" src=http://www.mamicode.com/"{{ row.blog.user.avatar }}"></a>
                                    <a class="a1">{{ row.summary }}</a>
                                </div>
                                <a class="a1">
                                    <p><i class="fa fa-user-o" aria-hidden="true"></i>
                                        {{ row.blog.user.username }} 发布于 2017-07-10
                                        <i class="fa fa-commenting-o" aria-hidden="true"></i>
                                        {{ row.comment_count }}
                                        <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>
                                        {{ row.up_count }}
                                    </p>
                                    <hr style="border:0.5px dotted darkgray"/>
                                </a>

                            {% endfor %}
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    {{ page_info.pager|safe }}
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-heading">Panel heading without title</div>
                        <div class="panel-body">
                            <p>人生若只如初见,何事秋风悲画扇</p>
                            <p>等闲变却故人心,却道故人心易变</p>
                            <p>骊山语罢清宵半,泪雨霖铃终不怨</p>
                            <p>何如薄幸锦衣郎,比翼连枝当日愿</p>
                        </div>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">Panel heading without title</div>
                        <div class="panel-body">
                            <p>十年生死两茫茫,不思量,自难忘</p>
                            <p>千里孤坟,无处话凄凉</p>
                            <p>纵使相逢应不识,尘满面,鬓如霜</p>
                            <p>夜来幽梦忽还乡,小轩窗,正梳妆</p>
                            <p>相顾无言,惟有泪千行</p>
                            <p>料得年年肠断处,明月夜,短松冈</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{#{% else %}#}
{#    <div>#}
{#        <div class="container">#}
{#            <div class="col-md-8">#}
{#                <div class="article-list">#}
{#                    <div class="article-item">#}
{#                        {% for row  in a_list %}#}
{#                            <div><a class="a1">{{ row.blog__article__title }}</a></div>#}
{#                            <div class="art-body clearfix">#}
{#                                <a><img class="left a1" src="http://www.mamicode.com/{{ row.avatar }}"></a>#}
{#                                <a class="a1">{{ row.blog__article__summary }}</a>#}
{#                            </div>#}
{#                            <a class="a1">#}
{#                                <p><i class="fa fa-user-o" aria-hidden="true"></i>#}
{#                                    {{ row.username }} 发布于 2017-07-10#}
{#                                    <i class="fa fa-commenting-o" aria-hidden="true"></i>#}
{#                                    {{ row.blog__article__comment_count }}#}
{#                                    <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>#}
{#                                    {{ row.blog__article__up_count }}#}
{#                                </p>#}
{##}
{#                                <hr style="border:0.5px dotted darkgray"/>#}
{#                            </a>#}
{#                        {% endfor %}#}
{#                        <nav aria-label="Page navigation">#}
{#                            <ul class="pagination">#}
{#                                {{ page_info.pager|safe }}#}
{#                            </ul>#}
{#                        </nav>#}
{#                    </div>#}
{#                </div>#}
{#            </div>#}
{#            <div class="col-md-4">#}
{#                <div class="panel panel-default">#}
{#                    <div class="panel-heading">Panel heading without title</div>#}
{#                    <div class="panel-body">#}
{#                        <p>人生若只如初见,何事秋风悲画扇</p>#}
{#                        <p>等闲变却故人心,却道故人心易变</p>#}
{#                        <p>骊山语罢清宵半,泪雨霖铃终不怨</p>#}
{#                        <p>何如薄幸锦衣郎,比翼连枝当日愿</p>#}
{#                    </div>#}
{#                </div>#}
{#                <div class="panel panel-default">#}
{#                    <div class="panel-heading">Panel heading without title</div>#}
{#                    <div class="panel-body">#}
{#                        <p>十年生死两茫茫,不思量,自难忘</p>#}
{#                        <p>千里孤坟,无处话凄凉</p>#}
{#                        <p>纵使相逢应不识,尘满面,鬓如霜</p>#}
{#                        <p>夜来幽梦忽还乡,小轩窗,正梳妆</p>#}
{#                        <p>相顾无言,惟有泪千行</p>#}
{#                        <p>料得年年肠断处,明月夜,短松冈</p>#}
{#                    </div>#}
{#                </div>#}
{#            </div>#}
{#        </div>#}
{#    </div>#}
{#{% endif %}#}
</body>
</html>
用户主页面内容.html

用户注销函数

def user_out(request):
    request.session.delete()
    return redirect(‘/boke/‘)
技术分享
import os
path_lis = []
def registered(request):
    if request.method==GET:
        return render(request,registered.html)
    else:
        if request.session.get(yzm).upper() == request.POST.get(yzm).upper():
            obj = User_idne(request.POST)
            if obj.is_valid():
                dic = obj.cleaned_data.pop(pwds)
                if dic==obj.cleaned_data[password]:
                    path_liss=\\+ path_lis[-1]
                    print(path_liss)
                    obj.cleaned_data[avatar]=path_liss
                    models.UserInfo.objects.create(**obj.cleaned_data)
                    return redirect(/login/)
                return render(request,registered.html,{mgs:俩次密码不一致!})
            return render(request, registered.html, {obj: obj})
        return render(request, registered.html, {msg:验证码错误!})

def Avatars(request):
    path = request.FILES.get(fafafa)
    paths = os.path.join(static, path.name)
    print(paths)
    path_lis.append(paths)
    with open(paths, wb) as f:
        for chunk in path.chunks():
            f.write(chunk)
    return HttpResponse(paths)
用户注册 views
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
    <style>
        .login{
            width: 600px;
            margin: 0 auto;
            padding: 20px;
            margin-top: 90px;
        }
        .hide{
            display: none;
        }
        img{
        margin-top: -55px;
        margin-left: 272px;
        width: 80px;
        height: 80px;
        }
        .files{
            position: absolute;
            left: 780px;
            top: 440px;
        }
        .zhuce{
            margin-top: 11px;
        }
        .imgs{
            width: 120px;
            height: 30px;
            margin-left: 1px;
            margin-top: 1px;
        }
    </style>
</head>
<body>
<div class="login">
    <form id="f1"  class="form-horizontal"  method="POST" action="/registered/" enctype="multipart/form-data">
      {% csrf_token %}
       <div id="container3" class="imgs"></div>
      <div class="form-group">
        <label  class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
          <input type="text" class="form-control"  placeholder="用户名" name="username">{{ obj.errors.username.0 }}
        </div>
      </div>
        <div class="form-group">
        <label  class="col-sm-2 control-label">昵名</label>
        <div class="col-sm-10">
          <input type="text" class="form-control"  placeholder="昵名" name="nickname">{{ obj.errors.nickname.0 }}
        </div>
      </div>
      <div class="form-group">
        <label  class="col-sm-2 control-label">邮箱</label>
        <div class="col-sm-10">
          <input type="text" class="form-control"  placeholder="邮箱" name="email">{{ obj.errors.email.0 }}
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
          <input type="password" class="form-control"  placeholder="密码" name="password">{{ obj.errors.password.0 }}
        </div>
      </div>
        <div class="form-group">
        <label class="col-sm-2 control-label">确认密码</label>
        <div class="col-sm-10">
          <input type="password" class="form-control"  placeholder="确认密码" name="pwds">{{ obj.errors.pwds.0 }}{{ mgs }}
        </div>
      </div>
        <div class="form-group">
        <label class="col-sm-2 control-label">验证码</label>
        <div class="col-sm-5">
          <input type="text" class="form-control"  placeholder="验证码" name="yzm">
            {{ msg }}

        </div>
        <div class="col-sm-5">
          <img style="width: 120px;height: 30px;" src=http://www.mamicode.com/"/check_code/" class="imgs">
            &nbsp,&nbsp;
          <a href=http://www.mamicode.com/"/registered/">更换验证码</a>
        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <input type="button" onclick="zhuce()" class="btn btn-default zhuce" value=http://www.mamicode.com/"注册" />
        </div>

      </div>
    </form>
</div>
<div>
    <form id="f2"  class="form-horizontal"  method="POST" action="/Avatars/" target="ifr" enctype="multipart/form-data">
        {% csrf_token %}
       <div>
           <iframe class="hide" src=http://www.mamicode.com/"/Avatars/" name="ifr" id="ifr" frameborder="0"></iframe>
           <input type="file" name="fafafa" id="fafafa" class="files">
       </div>
    </form>

</div>
<script src=http://www.mamicode.com/"/static/jquery-3.2.1.js"></script>
<script>
    function zhuce() {
        $(#f1).submit()
    }

    $(#fafafa).change(function () {
        document.getElementById(ifr).onload=Isitsgs;
        $(#f2).submit()
    });

    function Isitsgs() {
         var content =document.getElementById(ifr).contentWindow.document.body.innerText;
{#查找id是ifr的标签,的window内容的请求体的文本信息#}
         var tag = document.createElement(img);
{#创建一个img标签并设置标签名字是tag#}
         tag.src=http://www.mamicode.com//+content;
{#找到名字是tag标签设置路径#}
         $(#container3).append(tag);
{#找到id 是container3的标签添加一个名字是tag的标签tag#}
    }
</script>
</body>
</html>
用户注册 html
技术分享
def bokes(request,user):
    if request.method==GET:
        userss = models.UserInfo.objects.filter(username=user,).values(
            avatar,
            username,
            blog__article__title,
            blog__article__comment,
            blog__article__summary,
            blog__article__comment_count,
            blog__article__up_count,
            blog__tag__title,
            blog__category__title,
            blog__tag__article2tag,
        )
        print(userss[0][avatar])
        # request.session[‘centen‘]={‘k‘:‘v‘}
        article_list = models.UserInfo.objects.filter(username=user).count()
        page_info = PageInfo(request.GET.get(page), article_list, 10, /home.html, 11)
        # print(page_info.start())
        article_list = models.UserInfo.objects.filter(username=user)[
                       page_info.start():page_info.end()]
        # return redirect(‘/boke/‘)
        print(userss[0][blog__tag__article2tag])
        return render(request,bokes.html,{
            touxiang:userss[0][avatar],
            a_list:userss,
            article_list: article_list,
            page_info:page_info,
            tag:userss[0][blog__tag__title],
            # ‘tag_count‘:userss[0][‘blog‘],
            category:userss[0][blog__category__title],


        })
用户个人主页 views
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" />
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/font-awesome-4.7.0/css/font-awesome.css" />
    <link rel="stylesheet" href=http://www.mamicode.com/"/static/css/commons.css"/>
    <style>
        .touxiang{
        width:48px;
        height: 48px;
        border-radius: 50%;
}       .container{
            width: auto;
                    height:auto;
                }
    </style>
</head>
<body>
<nav class="navbar navbar-default no-radius">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand a1" href=http://www.mamicode.com/"#">老子的技术论坛</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            {% if type_id %}
                <li><a class="a1" href=http://www.mamicode.com/"/">全部</a></li>
            {% else %}
                <li><a class="active" href=http://www.mamicode.com/"/">全部</a></li>
            {% endif %}
            {% for item in type_choice_list %}
                {% if item.0 == type_id %}
                    <li class="active"><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                {% else %}
                    <li><a class="a1" href=http://www.mamicode.com/"/boke/{{ item.0 }}/">{{ item.1 }}</a></li>
                {% endif %}
            {% endfor %}
        </ul>
    {% if request.session.users %}
      <ul class="nav navbar-nav navbar-right">
        <li><a class="a1" href=http://www.mamicode.com/"/boke/{{ request.session.users.users }}.html">我的博客</a></li>
        <li><a class="a1" href=http://www.mamicode.com/"#">{{ request.session.users.users }}</a></li>
        <li><a class="a1" href=http://www.mamicode.com/"/user_out/">注销</a></li>
        <li><img class="touxiang" src=http://www.mamicode.com/"{{ request.session.users.touxiang }}"></li>
      </ul>
    {% else %}
        <ul class="nav navbar-nav navbar-right">
        <li><a class="a1" href=http://www.mamicode.com/"/login/"> 登录</a></li>
        <li><a class="a1" href=http://www.mamicode.com/"/registered/">注册</a></li>
        <li><img class="touxiang" src=http://www.mamicode.com/"{{ touxiang }}"></li>
      </ul>
    {% endif %}
    </div>
  </div>
</nav>
<div>

    <div class="container">
        <div class="col-md-2">
            <ul>
                <li>{{ tag }}{{ tag_count }}</li>
                <li>{{ category }}</li>
            </ul>
        </div>
        <div class="col-md-7">
            <div class="article-list">
                <div class="article-item">
                    {% for row  in a_list %}
                        <div><a class="a1">{{ row.blog__article__title }}</a></div>
                        <div class="art-body clearfix">
                            <a><img class="left a1" src=http://www.mamicode.com/"{{ row.avatar }}"></a>
                            <a class="a1">{{ row.blog__article__summary }}</a>
                        </div>
                        <a class="a1">
                            <p><i class="fa fa-user-o" aria-hidden="true"></i>
                                {{ row.username }} 发布于 2017-07-10
                                <i class="fa fa-commenting-o" aria-hidden="true"></i>
                                {{ row.blog__article__comment_count }}
                                <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>
                                {{ row.blog__article__up_count }}
                            </p>

                            <hr style="border:0.5px dotted darkgray"/>
                        </a>
                    {% endfor %}
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            {{ page_info.pager|safe }}
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">Panel heading without title</div>
                <div class="panel-body">
                    <p>人生若只如初见,何事秋风悲画扇</p>
                    <p>等闲变却故人心,却道故人心易变</p>
                    <p>骊山语罢清宵半,泪雨霖铃终不怨</p>
                    <p>何如薄幸锦衣郎,比翼连枝当日愿</p>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">Panel heading without title</div>
                <div class="panel-body">
                    <p>十年生死两茫茫,不思量,自难忘</p>
                    <p>千里孤坟,无处话凄凉</p>
                    <p>纵使相逢应不识,尘满面,鬓如霜</p>
                    <p>夜来幽梦忽还乡,小轩窗,正梳妆</p>
                    <p>相顾无言,惟有泪千行</p>
                    <p>料得年年肠断处,明月夜,短松冈</p>
                </div>
            </div>
        </div>
    </div>
</div>
用户个人主页.html

 

python 保障系统(一)