美文网首页
归档, AboutMe和标签分类

归档, AboutMe和标签分类

作者: 阡陌3536 | 来源:发表于2017-08-30 14:24 被阅读0次

    归档

    归档就是列出当前博客中所有的文章, 并且能够显示时间, 很容易的可以写出对应的view和模板来

    在my_blog/my_blog/view下新建归档view

    defarchives(request):try:        post_list = Article.objects.all()exceptArticle.DoesNotExist :raiseHttp404returnrender(request,'archives.html', {'post_list': post_list,'error':False})

    在my_blog/templates新建模板archives.html

    {% extends "base.html" %}

    {% block content %}

    {% for post in post_list %}

    {{ post.title }}

    Time: 

    {% endfor %}

    {% endblock %}

    并在my_blog/my_blog/usls.py中添加对应url配置

    fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = patterns('',# Examples:# url(r'^$', 'my_blog.views.home', name='home'),# url(r'^blog/', include('blog.urls')),url(r'^admin/', include(admin.site.urls)),    url(r'^$','article.views.home', name ='home'),    url(r'^(?P\d+)/$','article.views.detail', name='detail'),    url(r'^archives/$','article.views.archives', name ='archives'),)

    AboutMe

    这个就不多说了

    在my_blog/my_blog/view.py下添加新的逻辑

    def about_me(request) :

    return render(request, 'aboutme.html')

    在my_blog/template下新建模板aboutme.html, 内容如下, 大家可以自定义自己喜欢的简介

    {% extends "base.html" %}

    {% load custom_markdown %}

    {% block content %}

    About Me 正在建设中

    {% endblock %}

    并在my_blog/my_blog/usls.py中添加对应url配置

    fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = patterns('',# Examples:# url(r'^$', 'my_blog.views.home', name='home'),# url(r'^blog/', include('blog.urls')),url(r'^admin/', include(admin.site.urls)),    url(r'^$','article.views.home', name ='home'),    url(r'^(?P\d+)/$','article.views.detail', name='detail'),    url(r'^archives/$','article.views.archives', name ='archives'),    url(r'^aboutme/$','article.views.about_me', name ='about_me'),)

    标签分类

    实现功能: 点击对应的标签按钮, 会跳转到一个新的页面, 这个页面是所有相关标签的文章的罗列

    只需要在在my_blog/my_blog/view.py下添加新的逻辑

    def search_tag(request, tag) :

    try:

    post_list = Article.objects.filter(category__iexact = tag) #contains

    except Article.DoesNotExist :

    raise Http404

    return render(request, 'tag.html', {'post_list' : post_list})

    可以看成是对tag的查询操作, 通过传入对应点击的tag, 然后对tag进行查询

    在对应的有tag的html网页中修改代码

    {% extends "base.html" %}

    {% load custom_markdown %}

    {% block content %}

    {% for post in post_list %}

    {{ post.title }}

    Time: 

    {{ post.content|custom_markdown }}

    Read More >>>

    {% endfor %}

    {% endblock %}

    仔细看这一句. 其中标签对超链接已经发生改变, 这是在对标签就行点击时, 会将标签作为参数, 传入到对应的view中执行逻辑, 然后进行网页跳转...

    并在my_blog/my_blog/usls.py中添加对应url配置

    fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = patterns('',# Examples:# url(r'^$', 'my_blog.views.home', name='home'),# url(r'^blog/', include('blog.urls')),url(r'^admin/', include(admin.site.urls)),    url(r'^$','article.views.home', name ='home'),    url(r'^(?P\d+)/$','article.views.detail', name='detail'),    url(r'^archives/$','article.views.archives', name ='archives'),    url(r'^aboutme/$','article.views.about_me', name ='about_me'),    url(r'^tag(?P\w+)/$','article.views.search_tag', name ='search_tag'),)

    现在在浏览器中输入http://127.0.0.1:8000/, 点击对应的归档或者ABOUT ME 或者标签按钮可以看到对应的效果

    相关文章

      网友评论

          本文标题:归档, AboutMe和标签分类

          本文链接:https://www.haomeiwen.com/subject/zgytjxtx.html