zoukankan      html  css  js  c++  java
  • django 开发简易博客(二)

      这一节我们来了解模板和视图、URL的使用。

      一.使用模板

      在blog目录中新建templates文件夹,在templates文件夹下新建base.html文件。目录结构如下

    templates/
        base.html
    View Code

      编写base.html文件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
            {% block title %}
            {% endblock %}
        </title>
    </head>
    <body>
        <h1>a simple blog</h1>
        {% block content %}
        {% endblock %}
        {% block footer %}
            <p>Thanks for visiting my site!</p>
        {% endblock %}
    </body>
    </html>
    View Code

      base.html定义了一个简单的html框架,接下来将在所有的页面中使用。

      在templates目录下新建blog_list.html

    {% extends "base.html" %}
    
    {% block title %} blog list {% endblock %}
    
    
    {% block content %}
    <div class="content">
        {% for blog in blogs %}
            <h3>{{ blog.caption }}</h3>
            <div>{{ blog.content}} </div> 
        {% endfor %}     
    </div>    
    {% endblock %}
    View Code
      二.配置视图文件

      在blog目录中编辑views.py

    from django.shortcuts import render_to_response
    from blog.models import Blog
    
    
    def blog_list(request):
        blogs = Blog.objects.all()
        return render_to_response("blog_list.html", {"blogs": blogs})
    View Code
      三.配置url

      在web目录中编辑urls.py

    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'web.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),
    
        url(r'^admin/', include(admin.site.urls)),
        url(r'^web/',include('blog.urls')),
    )
    View Code

      在blog目录中添加urls.py 文件

    from django.conf.urls import *
    
    urlpatterns = patterns(('blog.views'),
        url(r'^bloglist/$', 'blog_list', name='bloglist'), 
    )
    View Code

      使用admin添加几个tag、blog、和author,最后运行服务器,打开127.0.0.1:8000/web/bloglist,显示如下界面

         

      四、添加博客展示页面

      在templates目录中添加blog_show.html。

    {% extends "base.html" %}
    
    {% block title %} {{ blog.caption }} {% endblock %}
    
    
    {% block content %}
    <div class="content">
      <h2>blog show</h2>
        <h4>{{ blog.caption }}</h4>
        <div>{{ blog.content }} </div>
    </div>    
    {% endblock %}
    View Code

      在views.py文件中添加blog_show视图函数

    from django.http import Http404
    
    def blog_show(request, id=''):
        try:
            blog = Blog.objects.get(id=id)
        except Blog.DoesNotExist:
            raise Http404
        return render_to_response("blog_show.html", {"blog": blog})
    View Code

      修改blog目录下面的urls.py ,添加如下内容

    url(r'^blog/(?P<id>d+)/$','blog_show',name='detailblog'),

      修改blog_list.html

     <h3>{{ blog.caption }}</h3>

      将其改为

     <h3>
        <a href="{% url 'detailblog' blog.id %}">
            {{ blog.caption }}
        </a>
    </h3>

      特别注意的是detailblog一定要带上单引号,再次刷新下blog_list页面,博客标题就便成了链接。

      

     

      

      

      

  • 相关阅读:
    MySql锁机制
    Mysql存储引擎
    Linux 系统中安装mysql
    常见的系统架构
    Linux环境下搭建go开发环境
    Ajax概述
    正向代理和反向代理
    Mysql 存储过程以及在.net中的应用示例
    Mysql 事务
    Windows服务器实现自动化部署-Jenkins
  • 原文地址:https://www.cnblogs.com/fireflow/p/4713297.html
Copyright © 2011-2022 走看看