简体   繁体   English

'Request'对象没有属性'META'

[英]'Request' object has no attribute 'META'

Here is my view: 这是我的观点:

def data(request, symbol):
   context_dict = {}

   NASDAQ = "http://www.nasdaq.com/symbol/{}/financials?query=income-statement".format(symbol)

   import urllib.request
   from bs4 import BeautifulSoup

   user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
   headers = {'User-Agent': user_agent, }
   request = urllib.request.Request(NASDAQ, None, headers)  # The assembled request
   response = urllib.request.urlopen(request)
   html_data = response.read()  # The data u need

   soup = BeautifulSoup(html_data)
   genTable = soup.find_all("div", class_="genTable")

   context_dict['genTable'] = genTable

   return render(request, 'data.html', context_dict)

When I return HttpResponse , there is no error. 当我返回HttpResponse ,没有错误。

I'm trying to render the context_dict above into data template. 我正在尝试将上面的context_dict渲染到数据模板中。 This gives me 'Request' object has no attribute Meta . 这给了我'Request' object has no attribute Meta How do I fix this? 我该如何解决?

You replaced the request object passed to your view by a local variable in the line 您通过行中的局部变量替换了传递给视图的请求对象

request = urllib.request.Request(NASDAQ, None, headers)  # The assembled request

Name this variable something else. 将此变量命名为其他内容。 Like 喜欢

assembled_request = urllib.request.Request(NASDAQ, None, headers)  # The assembled request
response = urllib.request.urlopen(assembled_request)

You have reassigned django's request with the return value from urllib, which is why your other lines are not working: 你已经用urllib的返回值重新分配了django的请求,这就是为什么你的其他行无效:

request = urllib.request.Request(NASDAQ, None, headers)

Change the above line so that it evaluates to something other than request . 更改上面的行,使其评估为除request其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM