视图与模版-django

视图与模版-django

视图

在上一章使用django-admin.py startproject制作的mysite文件夹中,创建一个叫做views.py的空文件。
并输入以下代码

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello world")

一个视图就是Python的一个函数。这个函数第一个参数的类型是HttpRequest;它返回一个HttpResponse实例。为了使一个Python的函数成为一个Django可识别的视图,它必须满足这两个条件。

URL配置

修改urls.py

from django.conf.urls import include, url
from django.contrib import admin
#from django.http import HttpResponse
from mysite.views import hello
#def hello(request):
#    return HttpResponse('hello  world')
urlpatterns = [
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', hello),
]

打开浏览器http://ip:8080即可看到输出的hello world
如果 url(r'^$', hello),改为 url(r'^hello/$', hello),
则需要输入http://ip:8080/hello/才能输出hello world

模版

  • 修改模版路径,
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

修改为

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['/home/linaro/temp/mysite/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',
            ],
        },
    },
]

同时需要在项目的根目录下(/home/linaro/temp/mysite/)新建文件夹template,
并新建模版info.html,代码如下:

<html>
<head>
<title>测试Django模板</title>
</head>
<body>
<p>名字:{{name}}.</p>
<p>性别:{{sex}}.</p>
<p>年龄:{{age}}.</p>
</body>
</html>
  • 由于该模版有中文字符,为了避免中文字符带来的乱码问题,最好将模版文件的编码改为uft-8,同时在settings.py文件添加一下代码:
FILE_CHARSET = 'utf-8'
DEFAULT_CHARSET = 'utf-8'`

同时在views.py下添加函数,并导入from django.shortcuts import render_to_response:

def info(request):
    return render_to_response('info.html', {'name': 'liton','sex':'man','age':'18'})

整个views.py的代码此时如下

from django.http import HttpResponse
from django.shortcuts import render_to_response
import datetime
def hello(request):
    return HttpResponse("Hello world")
def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)
def info(request):
    return render_to_response('info.html', {'name': 'liton','sex':'man','age':'18'})
  • 修改urls文件
from django.conf.urls import include, url
from django.contrib import admin
#from django.http import HttpResponse
from mysite.views import hello
from mysite.views import current_datetime
#def hello(request):
#    return HttpResponse('hello  world')
urlpatterns = [
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/$', hello),
    url('^time/$', current_datetime),
    url('^info/$', 'mysite.views.info'),
]

这里没有像hello,time视图采用from的形式导入函数,而是直接在url规则中,把路径写入。
此时打开http://ip:8080/info/即可看到一下信息

名字:liton.
性别:man.
年龄:18.
posted @ 2015-01-21 10:29  liton  阅读(167)  评论(0编辑  收藏  举报