美文网首页Django学习技术硬通货python
Django搭建简易博客教程(七)-Template

Django搭建简易博客教程(七)-Template

作者: Andrew_liu | 来源:发表于2015-01-04 11:45 被阅读4977次

整个项目已经放在Github上, 随时更新, 项目地址


貌似Markdown解析出问题了, @简书

Template初探


到目前为止我们只是简单的将后端数据显示到页面上, 没有涉及到HTML代码, 而优雅的网站总算通过CSS+HTML, 甚至还有强大的JS的支持.

在这个教程中要打造一个Blog, 所以我们设置一个Blog界面, 原本打算使用Bootstrap作为前段的工具, 不过经过@游逸的建议, 使用了更加轻量级的Pure, 同样是响应式页面设置, 这也将是未来的主流吧..

在my_blog下添加文件名, 文件夹名为templates

mkdir templates
#看到当前文件构成
my_blog
├── article
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── admin.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── admin.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-34.pyc
│   │       └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── my_blog
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates

在my_blog/my_blog/setting.py下设置templates的位置

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates').replace('\\', '/'),
    )

意思是告知项目templates文件夹在项目根目录下

第一个template


templates/test.html简单第一个 template html文件

<!--在test.html文件夹下添加-->
<!DOCTYPE html>
<html>
    <head>
        <title>Just test template</title>
        <style>
            body {
               background-color: red;
            }
            em {
                color: LightSeaGreen;
            }
        </style>
    </head>
    <body>
        <h1>Hello World!</h1>
        <strong>{{ current_time }}</strong>
    </body>
</html>

其中{{ current_time }}是Django Template中变量的表示方式

在article/view.py中添加一个函数逻辑

from django.shortcuts import render
from django.http import HttpResponse
from article.models import Article
from datetime import datetime

# Create your views here.
def home(request):
    return HttpResponse("Hello World, Django")

def detail(request, my_args):
    post = Article.objects.all()[int(my_args)]
    str = ("title = %s, category = %s, date_time = %s, content = %s" 
        % (post.title, post.category, post.date_time, post.content))
    return HttpResponse(str)

def test(request) :
    return render(request, 'test.html', {'current_time': datetime.now()})

render()函数中第一个参数是request 对象, 第二个参数是一个模板名称,第三个是一个字典类型的可选参数. 它将返回一个包含有给定模板根据给定的上下文渲染结果的 HttpResponse对象。

然后设置对应的url在my_blog/urls.py下

    url(r'^test/$', 'article.views.test'),

重新启动服务器python manage.py runserver, 然后在浏览器中输入http://127.0.0.1:8000/test/, 可以看到

testtest

正式编写template


在template文件夹下增加base.html, 并在其中增加如下代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A layout example that shows off a blog page with a list of posts.">

    <title>Andrew Liu Blog</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-min.css">
    <link rel="stylesheet" href="http://picturebag.qiniudn.com/blog.css">
</head>
<body>
<div id="layout" class="pure-g">
    <div class="sidebar pure-u-1 pure-u-md-1-4">
        <div class="header">
            <h1 class="brand-title">Andrew Liu Blog</h1>
            <h2 class="brand-tagline">雪忆 - Snow Memory</h2>
            <nav class="nav">
                <ul class="nav-list">
                    <li class="nav-item">
                        <a class="pure-button" href="https://github.com/Andrew-liu">Github</a>
                    </li>
                    <li class="nav-item">
                        <a class="pure-button" href="http://weibo.com/dinosaurliu">Weibo</a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>

    <div class="content pure-u-1 pure-u-md-3-4">
        <div>
            {% block content %}
            {% endblock %}
            <div class="footer">
                <div class="pure-menu pure-menu-horizontal pure-menu-open">
                    <ul>
                        <li><a href="http://andrewliu.tk/about/">About Me</a></li>
                        <li><a href="http://twitter.com/yuilibrary/">Twitter</a></li>
                        <li><a href="http://github.com/yahoo/pure/">GitHub</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>

上面这段html编写的页面是一个模板, 其中{% block content %} {% endblock %}字段用来被其他继承这个基类模板进行重写

我们继续在templates文件夹下添加home.html文件

{% extends "base.html" %}

{% block content %}
<div class="posts">
    {% for post in post_list %}
        <section class="post">
            <header class="post-header">
                <h2 class="post-title">{{ post.title }}</h2>

                    <p class="post-meta">
                        Time:  <a class="post-author" href="#">{{ post.date_time }}</a> <a class="post-category post-category-js" href="#">{{ post.category }}</a>
                    </p>
            </header>

                <div class="post-description">
                    <p>
                        {{ post.content }}
                    </p>
                </div>
        </section>
    {% endfor %}
</div><!-- /.blog-post -->
{% endblock %}      

其中

  • {% for <element> in <list> %}与{% endfor %}成对存在, 这是template中提供的for循环tag
  • {% if <elemtnt> %} {% else %} {% endif %}是template中提供的if语句tag
  • template中还提供了一些过滤器

然后修改my_blog/article/view.py, 并删除test.html

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from article.models import Article
from datetime import datetime

# Create your views here.
def home(request):
    post_list = Article.objects.all()  #获取全部的Article对象
    return render(request, 'home.html', {'post_list' : post_list})

修改my_blog/my_blog/urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = 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'),
)

现在重新打开http://127.0.0.1:8000/, 发现Blog的整理框架已经基本完成, 到现在我们已经了解了一些Django的基本知识, 搭建了简单地Blog框架, 剩下的就是给Blog添加功能

基本框架基本框架

查看当前整个程序的目录结构

my_blog
├── article
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── admin.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── admin.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-34.pyc
│   │       └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── my_blog
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates
    ├── base.html
    └── home.html

将代码上传到Github


在github中新建仓库my_blog_tutorial, 填写简单的描述

#查看当前目录位置
$ pwd
/Users/andrew_liu/Python/Django/my_blog

#在项目的根目录下初始化git
git init
Initialized empty Git repository in/Users/andrew_liu/Python/Django/my_blog/.git/

#添加远程github
$ git remote add blog git@github.com:Andrew-liu/my_blog_tutorial.git

在根目录下增加`.gitignore'和'LICENSE'和'README.md'文件

#添加所有文件
$ git add .

#查看当前状态
$ git status

#commit操作
$ git commit -m "django tutorial init"

#上传github
$ git push -u blog master
Counting objects: 23, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (22/22), done.
Writing objects: 100% (23/23), 19.56 KiB | 0 bytes/s, done.
Total 23 (delta 1), reused 0 (delta 0)
To git@github.com:Andrew-liu/my_blog_tutorial.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from blog.

相关文章

网友评论

  • 99774734cf50: 路径设置需要修改一下,高版本需要改成
    TEMPLATES = [


    'DIRS': [os.path.join(BASE_DIR, 'templates')],
  • 99774734cf50:Remove TEMPLATE_DIRS from settings.py

    Add standard Django 1.8.x template settings

    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(MAIN_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
    'context_processors': [
    'django.template.context_processors.debug',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    ],
    },
    },
    ]
  • 7699f7be7c72:我照着搭了一遍,很顺利, 我也要开始搭建有自己个性的博客了,哈哈哈哈
  • d20f9f3da10e: @Andrew_liu 好的,我今晚看,不过我已经正确加载模板了,今天下午还遇到了外部样式表加载不了的事儿,然后拿static一顿改居然也可以了,感谢您,我去您博客看看哈
  • Andrew_liu:@qingyu1989 1.8.2的设置已经改了, 你看一下我的博客, 博客的文章已经改了, replace在1.82有问题, 我博文对应章节已经修改

    2
    3
    4
    5
    #尝试这种写法
    TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
    TEMPLATE_DIRS = (
    TEMPLATE_PATH,
    )
    bb14bdc275a8:@Andrew_liu 1.9都是写在TEMPLATES的DIRS了
    东瓜王:@Andrew_liu 1.8.2中,template的设置已经移动到TEMPLATES的DIRS
  • d20f9f3da10e:@Andrew_liu 您看,我是把这个值填上了。。。'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/')],以后还希望能跟您多交流
  • d20f9f3da10e: @Andrew_liu 谢谢您,我再看一遍,而且昨天晚上我已经试了一个地方,就是dir那个空列表,我填上了项目路径,已经好用了,太晚了,本来打算今天回复您的,还是谢谢您的帮助~
  • Andrew_liu:@qingyu1989 1.8.2的设置已经改了, 你看一下我的博客, 博客的文章已经改了
  • d20f9f3da10e:大神我看了出错信息了,这个项目运行的时候,默认是去C:\Python34\Lib\site-packages\django\contrib\admin\templates这个文件夹下找html文件,可是却不在我的项目中的Templates文件夹中寻找,这应该怎么设置呢?我用的是DJango1.8.2
  • d20f9f3da10e:大神,我一步一步做的,但是做到显示test的时候,就出现了TemplateDoesNotExist at /test/
    test.html,怎么调都不会。。。怎么回事儿呀?,谢谢您~

本文标题:Django搭建简易博客教程(七)-Template

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