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
相關文章
相關標籤/搜索