Python(6)——Django之hello python模板视图

     在上一篇文章中简单的写了个hello python,但那中写法显然不符合mvc设计原则(Django也是mvc架构),下面我们来看看mvc架构的写法:

   1、在之前项目基础上新建:templates及index.html

Python(6)——Django之hello python模板视图

2、新建后在setting.py中修改相应配置:
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
)
3、在index.py中改相应代码:

from django.template.loader import get_template  
from django.template import Context  
from django.http import HttpResponse  

  
def current_datetime(request):  
    t = get_template('index.html')  
    html = t.render(Context({'msg': 'hello python!'}))  
    return HttpResponse(html)



或者:

from django.shortcuts import render_to_response  


def wel(request):
    return  render_to_response('index.html', {'msg': 'hello python!'})

4、 运行即可得到hello python。

我的博客其他文章列表  
http://my.oschina.net/helu






你可能感兴趣的:(Python(6)——Django之hello python模板视图)