Django视图与模板的学习基础

今晚更新~

views.py

def movie(url):
    tagUrl_read=urllib.urlopen(url).read()
    return tagUrl_read

def subject(tagUrl_read): 
    nameURL = re.findall('(http://movie.douban.com/subject/[0-9.]+)\/"\s+title=".+"',tagUrl_read)
    nameMovie = re.findall(r'http://movie.douban.com/subject/[0-9.]+\/"\s+title="(.+)"',tagUrl_read)
    scoreURL = re.findall(r'<span\s+class="rating_nums">([0-9.]+)<\/span>',tagUrl_read)
    evaluateURL = re.findall(r'<span\s+class="pl">\((\w+)人评价\)<\/span>',tagUrl_read)
    movieLists = list(zip(nameMovie,nameURL,scoreURL,evaluateURL))
    return movieLists

def index1(req):
    #movie_type = urllib.request.quote(input('请输入电影类型(如剧情、喜剧、悬疑):'))
    movie_type = '喜剧'
    #page_end=int(input('请输入搜索结束时的页码:'))
    page_end = 1
    newlist=[]
    for page in range(page_end):
        num=page*20
        url=r'http://movie.douban.com/tag/%s?start=%d'%(movie_type,num)
        urlread = movie(url)
        lists = subject(urlread)
        newlist.extend(lists)
    book_list = sorted(newlist, key=lambda movieList : movieList[2],reverse = True)
    return render_to_response('index.html',{'book_list':book_list})

主要是模板的渲染使用render_to_response

第一个参数是模板html名称,第二个参数是字典形式,理解为:想模板传值

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> yshu </title>
</head>
<body>
      
{% for book in book_list %}
    <h5>
    <a href="{{ book.1 }}">{{book.0}}</a>
    </h5>
    
{% endfor %}
 
</body>
</html>

 模板标签  {{}}  {% %}  {{endif}}  {{endfor}}

posted on 2014-07-05 09:15  小与  阅读(140)  评论(0)    收藏  举报

导航