python Django注册页面显示头像
python Django注册页面显示头像(views)
def register(request): ''' 注册 :param request: :return: ''' if request.method=='GET': obj = Register(request) return render(request, 'register.html', {'obj': obj}) else: obj = Register(request,request.POST,request.FILES) if obj.is_valid(): print(obj.cleaned_data) yzms_list = obj.cleaned_data.pop('yzm') yzms1_list = obj.cleaned_data.pop('password2') users=models.UserInfo.objects.filter(username=obj.cleaned_data['username']) emails=models.UserInfo.objects.filter(email=obj.cleaned_data['email']) if users or emails: return render(request, 'register.html', {'obj': obj,'mmg':'用户已存在!','eml':'邮箱已注册!'}) else: if avatar_lis: obj.cleaned_data['avatar']=avatar_lis[-1] models.UserInfo.objects.create(**obj.cleaned_data) return redirect('/login/') else: obj.cleaned_data['avatar'] = '\static\8.png' models.UserInfo.objects.create(**obj.cleaned_data) return redirect('/login/') return render(request,'register.html', {'obj': obj})
============================================================================html=========================================================================
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <style> .login{ width: 600px; margin: 0 auto; padding: 20px; margin-top: 80px; } .f1{ position: absolute;height:80px;width: 80px;top:0;left: 0;opacity: 0; } </style> </head> <body> <div class="login"> <h3>用户注册</h3> <form id="fs" class="form-horizontal" action="/Avatar/" method="POST" target="ifr" enctype="multipart/form-data" novalidate > {% csrf_token %} <div style="position: relative;height:80px;width: 80px;"> <img id="previewImg" style="height:80px;width: 80px;" src="/static/8.png"> <iframe style="display: none" src="/Avatar/" id="ifr" name="ifr" frameborder="0"></iframe> {{ obj.avatar }} </div> </form> <form class="form-horizontal" method="POST" action="/register/" novalidate > {% csrf_token %} <div class="form-group"> <label class="col-sm-2 control-label">用户名</label> <div class="col-sm-10"> {{ obj.username }} {{ mmg }} {{ obj.errors.username.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">昵称</label> <div class="col-sm-10"> {{ obj.nickname }} {{ obj.errors.nickname.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">邮箱</label> <div class="col-sm-10"> {{ obj.email }} {{ eml }} {{ obj.errors.email.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">密码</label> <div class="col-sm-10"> {{ obj.password }} {{ obj.errors.password.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">密码</label> <div class="col-sm-10"> {{ obj.password2 }} {{ obj.errors.password2.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">验证码</label> <div class="col-sm-5"> {{ obj.yzm }} {{ obj.errors.yzm.0 }} </div> <div class="col-sm-5"> <img onclick="imgs(this)" style="width: 120px;height: 30px;" src="/check_code/" title="点击更新图片"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-default" value="注册"/> </div> </div> </form> </div> <script src="/static/jquery-3.2.1.js"></script> <script> function imgs(ths) { ths.src = ths.src+'?' } $(function () { bindAvatar1() }); function bindAvatar() { if (window.URL.createObjectURL){ bindAvatar2() }else if (window.FileReader){ bindAvatar3() }else{ bindAvatar1() } 这是一个流程使用控制,先进行检测如果浏览器支持第一种就执行第一种,以次类推之到最后的方式(这个方式不会出现浏览器不兼容现象) } Ajax上传方式(需要把头像发送到后端在获取) function bindAvatar1() { $('#imgSelect').change(function () { var obj = $(this)[0].files[0]; document.getElementById('ifr').onload=Idndfes; $('#fs').submit() }) } function Idndfes() { var content =document.getElementById('ifr').contentWindow.document.body.innerText; tag='/'+content; $('#previewImg').attr('src',tag) } 本地上传预览(在当前就能预览) function bindAvatar2() { $('#imgSelect').change(function () { var obj = $(this)[0].files[0]; // Ajax发送后台,并获取路径 // img.src = 获取路径 var v=window.URL.createObjectURL(obj); $('#previewImg').attr('src',v); $('#previewImg').load(function () { window.URL.revokeObjectURL(v) }) }) } function bindAvatar3() { $('#imgSelect').change(function () { var obj = $(this)[0].files[0]; // Ajax发送后台,并获取路径 // img.src = 获取路径 var reader = new FileReader(); reader.onload = function () { $('#previewImg').attr('src',this.result) }; reader.readAsDataURL(obj); }) } </script> </body> </html>
标签分组(views)
def tag_s(request,tag_nick,site): ''' 标签分组 :param request: :param tag_nick: :param site: :return: ''' tag_list=models.Tag.objects.filter(title=tag_nick,blog__site=site) print(tag_list) return render(request,'tag_s.html',{'tag_list':tag_list})
标签分组(html)
{% extends 'template.html' %} {% block ys %} {% endblock %} {% block xxs %} <h3>标签文章</h3> {% for i in tag_list %} {% for row in i.article2tag_set.all %} <h3><a href="/xiangxi/{{ request.session.user.user }}/{{ row.article.nid }}/">{{ row.article.title }}</a></h3> <div class="art-body clearfix"> <img style="float: left" src="{{ row.article.blog.user.avatar }}" alt=""> <a href="/xiangxi/{{ request.session.user.user }}/{{ row.article.nid }}/">{{ row.article.summary }}</a> </div> <p><i class="fa fa-user-o" aria-hidden="true"></i> <a href="">{{ row.article.blog.user.username }}</a> 发布于 <a href="">{{ row.article.blog.user.create_time }}</a> <i class="fa fa-commenting-o" aria-hidden="true"></i> <a href="">{{ row.article.read_count }}</a> <i class="fa fa-thumbs-o-up" aria-hidden="true"></i> <a href="">{{ row.article.up_count }}</a> </p> <hr style="border:0.5px dotted darkgray"/> {% endfor %} {% endfor %} {% endblock %}
分类分组(views)
def fenlei(request,fenle_nick,site): ''' 分类分组 :param request: :param fenle_nick: :param site: :return: ''' print(fenle_nick,site) fenle_list=models.Category.objects.filter(title=fenle_nick,blog__site=site) print(fenle_list) return render(request,'fenlei.html',{'fenle_list':fenle_list})
分类分组(html)
{% extends 'template.html' %} {% block ys %} {% endblock %} {% block xxs %} <h3>分类文章</h3> {% for row in fenle_list %} {% for i in row.article_set.all %} <h3><a href="/xiangxi/{{ request.session.user.user }}/{{ i.nid }}/">{{ i.title }}</a></h3> <div class="art-body clearfix"> <img style="float: left; width:80px;height: 80px" src="{{ i.blog.user.avatar }}" alt=""> <h4><a href="/xiangxi/{{ request.session.user.user }}/{{ i.nid }}/">{{ i.summary }}</a></h4> </div> <p><i class="fa fa-user-o" aria-hidden="true"></i> <a href="">{{ i.blog.user.username }}</a> 发布于 <a href="">{{ i.blog.user.create_time }}</a> <i class="fa fa-commenting-o" aria-hidden="true"></i> <a href="">{{ i.read_count }}</a> <i class="fa fa-thumbs-o-up" aria-hidden="true"></i> <a href="">{{ i.up_count }}</a> </p> <hr style="border:0.5px dotted darkgray"/> {% endfor %} {% endfor %} {% endblock %}
个人主页(views)
def home(request,site): ''' 个人主页 :param request: :param site: :return: ''' tags_dit={} corts_dit={} times_dit={} tags=models.Article2Tag.objects.filter(tag__blog__site=site).values('tag','tag__title').annotate(c=Count(1)) 通过annotate进行组合分组 for i in tags: tags_dit[i['tag__title']]=i['c'] print(tags_dit) corts=models.Article.objects.filter(blog__site=site).values('category','category__title').annotate(x=Count(1)) for i in corts: corts_dit[i['category__title']]=i['x'] times=models.Article.objects.filter(blog__site=site).extra(select={'c':'strftime("%%Y-%%m")'}).values('c').annotate(cx=Count(1)) 使用sqllist数据库的一种转换格式(组合分组是使用的extra) for i in times: times_dit[i['c']]=i['cx'] fen=models.UserInfo.objects.filter(blog__site=site) guanzhu=models.UserFans.objects.filter(user__blog__site=site).values('follower').annotate(s=Count(1)) art_count=models.Article.objects.filter(blog__site=site).count() page_info=PageInfo(request.GET.get('page'),art_count,3,request.path_info) artli_s=models.Article.objects.filter(blog__site=site)[page_info.start():page_info.end()] return render(request,'user_home.html',{ 'page_info':page_info, 'artli_s':artli_s, 'tags_dit':tags_dit, 'corts_dit':corts_dit, 'times_dit':times_dit, 'fen':fen, 'guanzhu':guanzhu, })
个人主页(html)
{% extends 'template.html' %} {% block xs %} <li><a href="/">主页</a></li> <li><a href="/">{{ request.session.user.user }}</a></li> <li><a href="/user_out/">注销</a></li> <li><img style="width: 48px; height: 48px; border-radius: 50%;" src="{{ request.session.user.avatar }}" alt=""></li> {% endblock %} {% block gg %} {% for i in fen %} <P>昵称:{{ i.nickname }}</P> <P>粉丝:{{ i.fans.count }}</P> <P>关注:{{ guanzhu.count }}</P> <P>邮箱:{{ i.email }}</P> {% endfor %} {% endblock %} {% block bq %} {% for tag,tag2 in tags_dit.items %} <p><a href="/tag/{{ tag }}/{{ request.session.user.user }}/">{{ tag }}({{ tag2 }})</a></p> {% endfor %} {% endblock %} {% block fl %} {% for tag,tag2 in corts_dit.items %} <p><a href="/fenlei/{{ tag }}/{{ request.session.user.user }}/">{{ tag }}({{ tag2 }})</a></p> {% endfor %} {% endblock %} {% block sj %} {% for tag,tag2 in times_dit.items %} <p><a href="/time/{{ tag }}/{{ request.session.user.user }}/">{{ tag }}({{ tag2 }})</a></p> {% endfor %} {% endblock %} {% block xxs %} <h3>个人主页</h3> {% for row in artli_s %} <h3><a href="">{{ row.title }}</a></h3> <div class="art-body clearfix"> <img style="float: left" src="{{ row.blog.user.avatar }}" alt=""> <a href="">{{ row.summary }}</a> </div> <p><i class="fa fa-user-o" aria-hidden="true"></i> <a href="">{{ row.blog.user.username }}</a> 发布于 <a href="">{{ row.blog.user.create_time }}</a> <i class="fa fa-commenting-o" aria-hidden="true"></i> <a href="">{{ row.read_count }}</a> <i class="fa fa-thumbs-o-up" aria-hidden="true"></i> <a href="">{{ row.up_count }}</a> </p> <hr style="border:0.5px dotted darkgray"/> {% endfor %} {% endblock %}