0

我有用python django编写的代码来解析一个url并将标签数据发送到另一个html页面,所以我在views.py中编码,我需要使用块内容将数据发送到html页面。

代码:

视图.py

from django.shortcuts import render_to_response
import urllib  
import re 

def home(request):
    htmlf = urllib.urlopen("https://docs.djangoproject.com/en/dev/topics/http/urls") 
    htmlt = htmlf.read() 
    regex = '<title>(.+?)</title> ' 
    patt = re.compile(regex) 
    price = re.findall(patt,htmlt) 
    return render_to_response("home.html", {'hello': price} )

主页.html

<html>
<head>
    <title>My first app</title>
</head>
<body>
    <h1>Welcome to story time!<h1>
    {% block content %}
    {{hello}}
    {% endblock %}
</body>
</html>

这里的问题是我需要在 home.html 页面中打印 price 的内容。

4

1 回答 1

0

我想你正在使用Django 1.4+摆脱 longrender_to_response

from django.shortcuts import render

def home(request):
    htmlf = urllib.urlopen("https://docs.djangoproject.com/en/dev/topics/http/urls") 
    htmlt = htmlf.read() 
    regex = '<title>(.+?)</title> ' 
    patt = re.compile(regex) 
    price = re.findall(patt,htmlt) 
    return render(request, "home.html", {'hello': price} )
于 2013-09-18T16:04:42.250 回答