blog开发之首页展示
首页展示
主要展示所有用户的文章,先看代码:
def index(request):
article_list = models.Article.objects.all()
return render(request, 'blog/index.html', {'article_list': article_list})
def set_password(request):
if request.is_ajax():
res = BaseResponse()
if request.method == 'POST':
old_password = request.POST.get('old_password')
new_password = request.POST.get('new_password')
confirm_password = request.POST.get('confirm_password')
is_right = request.user.check_password(old_password)
if is_right:
if new_password == confirm_password:
request.user.set_password(new_password)
request.user.save()
res.data = '修改成功!'
else:
res.code = 3003
res.error = '两次密码输入不一致!'
else:
res.code = 3004
res.error = '原密码错误'
return JsonResponse(res.dict)
@login_required
def set_icon(request):
blog = request.user.blog
username = request.user.username
if request.method == 'POST':
avatar = request.FILES.get('avatarimg')
print(avatar)
if avatar:
request.user.avatar = avatar
request.user.save()
return redirect(reverse('blog:index'))
return render(request, 'blog/set_icon.html', {'blog': blog,'username': username})
def logout(request):
auth.logout(request)
return redirect(reverse('blog:login')
注意:
1.修改密码,修改头像后,记得别忘记save()。
index页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>博客首页</title> <link rel="stylesheet" href="/static/blog/bs/css/bootstrap.css"> <script src="/static/jquery-3.3.1.js"></script> <script src="/static/blog/bs/js/bootstrap.min.js"></script> </head> <body> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <!-- 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" href="#">博客园</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">随笔 <span class="sr-only">(current)</span></a></li> <li><a href="#">新闻</a></li> <li><a href="#">博文</a></li> </ul> <ul class="nav navbar-nav navbar-right"> {% if request.user.is_authenticated %} <li><a href="#"><span id="user_icon" class="glyphicon glyphicon-user"></span>{{ request.user.username }}</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#" data-toggle="modal" data-target=".bs-example-modal-lg">修改密码</a></li> <li><a href="{% url 'blog:set_icon' %}">修改头像 action</a></li> <li><a href="{% url 'blog:logout' %}">注销</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> </ul> <!-- Large modal --> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <h2 class="text-center">修改密码</h2> <div class="row"> <div class="col-md-6 col-md-offset-3"> <form> {% csrf_token %} <div class="form-group"> <label for="">用户名</label> <input type="text" disabled value="{{ request.user.username }}" class="form-control"> </div> <div class="form-group"> <label for="id_old_password">原密码</label> <input type="password" id="id_old_password" class="form-control"> </div> <div class="form-group"> <label for="id_new_password">新密码</label> <input type="password" id="id_new_password" class="form-control"> </div> <div class="form-group"> <label for="id_confirm_password">确认密码</label> <input type="password" id="id_confirm_password" class="form-control"> </div> <button type="button" class="btn btn-primary" id="id_edit">提交</button> <button type="button" class="btn btn-default" data-dismiss="modal"> Close </button> <span style="color: red;" id="password_error"></span> <hr> </form> </div> </div> </div> </div> </div> </li> {% else %} <li><a href="{% url 'blog:login' %}">登录</a></li> <li><a href="{% url 'blog:register' %}">注册</a></li> {% endif %} </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="container"> <div class="row"> <div class="col-md-3"> <div class="panel panel-default"> <div class="panel-heading">Panel heading without title</div> <div class="panel-body"> Panel content </div> </div> <div class="panel panel-warning"> <div class="panel-heading">Panel heading without title</div> <div class="panel-body"> Panel content </div> </div> <div class="panel panel-success"> <div class="panel-heading">Panel heading without title</div> <div class="panel-body"> Panel content </div> </div> </div> <div class="col-md-6"> <div class="article_list"> {% for article in article_list %} <div class="article_item"> <h5> <a href="{% url 'blog:article_detail' article.user.username article.pk %}">{{ article.title }}</a> </h5> <div class="article_desc"> <span class="media-left"> <a href="{% url 'blog:home_site' article.user.username %}"><img src="/blog/media/{{ article.user.avatar }}" width="56" height="56" alt=""></a> </span> <span class="media-right"> {{ article.desc }} </span> </div> <div class="small pub_info"> <span><a href="{% url 'blog:home_site' article.user.username %}">{{ article.user.username }}</a></span> <span>发布于: {{ article.create_time|date:'Y-m-d' }}</span> <span class="glyphicon glyphicon-comment"></span>评论({{ article.comment_count }}) <span class="glyphicon glyphicon-thumbs-up"></span>点赞({{ article.up_count }}) </div> </div> <hr> {% endfor %} </div> </div> <div class="col-md-3"> <div class="panel panel-primary"> <div class="panel-heading">Panel heading without title</div> <div class="panel-body"> Panel content </div> </div> <div class="panel panel-info"> <div class="panel-heading">Panel heading without title</div> <div class="panel-body"> Panel content </div> </div> </div> </div> </div> <script> $('#id_edit').click(function () { $.ajax({ url: "/blog/set_password/", type: 'post', data: { old_password: $("#id_old_password").val(), new_password: $('#id_new_password').val(), confirm_password: $('#id_confirm_password').val(), csrfmiddlewaretoken: $('[name="csrfmiddlewaretoken"]').val(), }, success: function (data) { console.log(data); if (data.code === 1000) { window.location.href = '/blog/login/' } else { $('#password_error').text(data.error); setTimeout(function () { $('#password_error').text('') }, 1500) } } }) }) </script> </body> </html>