在urls中加路径:
url(r'^$', views.index),目的是为了将主页的路径可以不加index,直接用域名访问比如:127.0.0.1:8000就能进入主页
新建index.html文件:
导入bootstrap
-在主页用bootstrap做一个导航条
(下面的登录状态和注销都用auth模块实现)
-如果用户登录了则显示用户名和注销
-如果用户没登录则显示登录和注册
-用row将整个页面分成三列
-左边放文章的分类(需要建两个新表一个大的类别表,一个文章分类表)
-SiteCategory(网站分类表,里面只需要一个name字段)
-SiteArticleCategory(文章类别表,与网站分类表是多对一关系,关系字段写在这张表中,与文章是一对多关系,关系字段写在文章表中)
-建立panel面板
-查询类别表将类别名显示在面板主体中
-在views中查询SiteCategory的全部记录cate_list=models.SiteCategory.objects.all()返回给前端
-网站分类为面板头再建面板(加上class=“cate_title”)
-反向查询文章类别表的所有记录cate.sitearticlecategory_set.all用for循环写入面板主体都写成a标签
-给文章分类加上hide使在没错左前文章分类是隐藏的
-给cate_title绑定mouseover事件实现鼠标放到cate_title上显示下面的文章分类
$(".cate_title").mouseover (function(){
$(this).next().slideDown("1000")})
-给cate_title的父级绑定mouseleave事件使鼠标移开整个面板隐藏文章类别
$(".cate_title").parent().mouseleave(function(){
$(this).children(".panel-footer").slideUp("1000")})
-在urls中添加新路径为了是文章分类起作用:url(r'^cate/(?P<sitearticle_category>.*)/$', views.index)
-在每条文章分类加链接<a href="/cate/{{ sitearticlecategory.name }}">{{ sitearticlecategory.name }}这样每次点文章分类时链接将是cate/文章类别名
这样能显示这个分类下的文章
-中间放文章概要
-判断路径中是否有分类
-如果有则用filter过滤在分类下的所有文章放在article_list中
-如果没有分类则查询文章表中的全部文章放在article_list中
-将article_list返回给前端进行渲染
在前端页面用for循环取出文章标题在分出一个row里边放文章标题
分一个row用来渲染文章概要和作者头像用两个col渲染
在最下面再分一个row用来渲染作者名,发布时间评论数点赞的数阅读数
-右边放其他
from django.conf.urls import url,include from django.contrib import admin from django.views.static import serve from blogCMS import settings from blog import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', views.login), url(r'^get_validCode_img/', views.get_validCode_img), url(r'^index/', views.index), url(r'^$', views.index), url(r'^logout/', views.log_out), url(r'^article/', views.article), url(r'^reg/', views.reg), url(r'^cate/(?P<sitearticle_category>.*)/$', views.index), #个人站点首页 url(r'^blog/', include("blog.urls")), # media 配置 url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), ]
def index(request,*args,**kwargs): if kwargs: article_list=models.Article.objects.filter(site_article_category__name=kwargs.get('sitearticle_category')) print("=========",article_list) else: article_list=models.Article.objects.all() cate_list=models.SiteCategory.objects.all() return render(request,"index.html",{"article_list":article_list,"cate_list":cate_list})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <script src="/static/dist/js/jquery-3.1.1.js"></script> <script src="/static/dist/js/bootstrap.js"></script> <link rel="stylesheet" href="/static/dist/css/bootstrap.css"> <style> .hides{ display:none } </style> </head> <body> <nav class="navbar navbar-inverse"> <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" href="#">Brand</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="#">Link <span class="sr-only">(current)</span></a></li> <li><a href="#">Link</a></li> </ul> <ul class="nav navbar-nav navbar-right"> {% if request.user.is_authenticated %} <li><a href=""> <span class="glyphicon glyphicon-user"></span>{{ request.user.username }}</a></li> <li><a href="/logout/">注销</a></li> {% else %} <li><a href="login/">登录</a></li> <li><a href="/reg/">注册</a></li> {% endif %} <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="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="container"> <div class="row"> <div class="col-md-2 col-lg-offset-1"> <div class="panel panel-success"> <div class="panel-heading">网站分类</div> <div class="panel-body"> {% for cate in cate_list %} <div class="panel panel-default"> <div class="panel-heading cate_title"> {{ cate.name }} </div> <div class="panel-footer hides"> {% for sitearticlecategory in cate.sitearticlecategory_set.all %} <p><a href="/cate/{{ sitearticlecategory.name }}">{{ sitearticlecategory.name }}</a></p> {% endfor %} </div> </div> {% endfor %} </div> </div> </div> <div class="col-md-7"> <div class="article_list row"> {% for article in article_list %} <div class="article_item"> <div class="title"><a href="#">{{ article.title }}</a></div> <div class="row"> <div class="avatar col-md-2"> <img src="{{ article.user.avatar.url }}" alt="" width="60" height="60"> </div> <div class="col-md-9"> <p>{{ article.desc }}</p> </div> </div> <div class="pub_info row"> <a href="/blog/{{ article.user.username }}">{{ article.user.username }}</a> 发布于 {{ article.create_time|date:"Y-m-d H:i" }} <a href=""><span class="glyphicon glyphicon-comment"></span>评论({{ article.comment_count }})</a> <a href=""><span class="glyphicon glyphicon-thumbs-up"></span>点赞({{ article.up_count }})</a> </div> </div> <hr> <p></p> {% endfor %} </div> </div> <div class="col-md-2"> <div class="panel panel-info"> <div class="panel-heading">网站分类</div> <div class="panel-body"> Panel content </div> </div> <div class="panel panel-primary"> <div class="panel-heading">网站分类</div> <div class="panel-body"> Panel content </div> </div> </div> </div> </div> <script> $(".cate_title").mouseover (function(){ $(this).next().slideDown("1000") }) $(".cate_title").parent().mouseleave(function(){ $(this).children(".panel-footer").slideUp("1000") }) </script> </body> </html>