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

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

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



二、新建后在setting.py中修改相应配置:
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
)
三、在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)



或者:
html

from django.shortcuts import render_to_response  


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

四、 运行便可获得hello python。

个人博客其余文章列表  
http://my.oschina.net/helu
相关文章
相关标签/搜索