'Request' object has no attribute 'META'
Asked Answered
W

2

11

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.

I'm trying to render the context_dict above into data template. This gives me 'Request' object has no attribute Meta. How do I fix this?

Woll answered 3/6, 2015 at 4:51 Comment(0)
A
18

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)
Amiss answered 3/6, 2015 at 5:1 Comment(1)
I hit a similar issue but with Generic View Classes. In my case the culprit was a model named Request which was replacing the view's request object.Showiness
M
2

You have reassigned django's request with the return value from urllib, which is why your other lines are not working:

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

Change the above line so that it evaluates to something other than request.

Mcchesney answered 3/6, 2015 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.