CS50 PSET7 Quote: 'NoneType' Error - python

I have been having some trouble with /quote in PSET 7 of CS50. Every time I go into the CS50 finance site, it returns:
AttributeError: 'NoneType' object has no attribute 'startswith'
I am not sure what it means, nor how to fix it. It seems to be automatically going to 'None' in the lookup function, but I am not sure why. If someone could help me, I would really appreciate it!
This is my quote part of application.py:
#app.route("/quote", methods=["GET", "POST"])
#login_required
def quote():
"""Get stock quote."""
if request.method == "POST":
symbol = request.args.get("symbol")
quote = lookup(symbol)
return render_template("quoted.html", name=quote)
else:
return render_template("quote.html")
This is my helpers.py, which is not supposed to be changed:
def lookup(symbol):
"""Look up quote for symbol."""
# reject symbol if it starts with caret
if symbol.startswith("^"):
return None
# reject symbol if it contains comma
if "," in symbol:
return None
# query Yahoo for quote
# http://stackoverflow.com/a/21351911
try:
url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
webpage = urllib.request.urlopen(url)
datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
row = next(datareader)
except:
return None
# ensure stock exists
try:
price = float(row[2])
except:
return None
# return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
return {
"name": row[1],
"price": price,
"symbol": row[0].upper()
}
Finally, this is my quote.html:
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="{{ url_for('quote') }}" method="post">
<fieldset>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="symbol" type="symbol"text"/>
</div>
<div class="form-group">
<button class="btn btn-default" type="submit">Search for Quote</button>
</div>
</fieldset>
</form>
{% endblock %}

That error would occur when there's no "symbol" parameter in the request.
symbol = request.args.get("symbol")
quote = lookup(symbol)
Because it's not present, .get(...) will return None, and when you call lookup(None) it will try to run the following line, with symbol as None:
if symbol.startswith("^"):
Which means you're trying to do None.startswith(...), explaining the error you see.
You could check for the case where symbol is missing/None and display an error message.
symbol = request.args.get("symbol")
if symbol:
quote = lookup(symbol)
return render_template("quoted.html", name=quote)
else:
return render_template("missing_symbol.html")
Or you could just ignore it: if there's no symbol, the request is probably invalid, and you can accept that it causes an error.

I managed to find the answer, I was supposed to put:
symbol = request.form.get("symbol") instead of:
symbol = request.args.get("symbol").

Related

How to give arguments to a html template?

I use four arguments in quoted.html, but when I try to pass them to from my application.py, there is an error:
TypeError: render_template() takes 1 positional argument but 4 were given
#app.route("/quote", methods=["GET", "POST"])
#login_required
def quote():
"""Get stock quote."""
#Look up for the stock symbol when user requires
if request.method == "POST":
#Check the symbol is none
if lookup(request.form.get("symbol")) == None:
return apology("The company's stock is not found!", 403)
#Show the stock result including name of the company and the stock price
else:
name = lookup(request.form.get("symbol"))["name"]
symbol = lookup(request.form.get("symbol"))["symbol"]
price = usd(lookup(request.form.get("symbol"))["price"])
return render_template("/quoted", name, symbol, price)
#Display the quote interface when user requires
else:
return render_template("quote.html")
Here is my quoted.html
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/quoted" method="get">
<fieldset>
<div class="form-group">
<h1> A share of {{ name }} ({{ symbol }}) costs {{ price }}</h1>
</div>
</fieldset>
</form>
{% endblock %}
Here is the return for the lookup function
def lookup(symbol):
"""Look up quote for symbol."""
# Parse response
try:
quote = response.json()
return {
"name": quote["companyName"],
"price": float(quote["latestPrice"]),
"symbol": quote["symbol"]
}
The render_template() takes one positional argument which is the html file. The rest of the data should be passed as keyword arguments.
Here is the documentation about it:
https://flask.palletsprojects.com/en/1.0.x/api/#flask.render_template
So you could do something like:
return render_template("quoted.html", name=name, symbol=symbol, price=price)
Or make one data structure with your data (dict, list) and work with it in your html

CS50 Finance writing to database issue

I am having another issue with CS50 finance and would really appreciate some help.
So I am working on the BUY section, and I have implemented an extra step, where you type in what stock you want, and how much of it you want, then it takes you to a new html page to confirm that you want to buy the stock. This pages tells you how many shares you will buy, how much it costs, and what your cash balance will be after you buy the shares.
My issue is that when I go to actually buy the stock on the confirmation page, I get errors that say my variables (specifically balance, shares and price_per_share) are undefined and I cannot figure out why. I will attached the relevant portions of my code below. Thanks again.
Application.PY:
#app.route("/buy", methods=["GET", "POST"])
#login_required
def buy():
if request.method == "POST":
quote = lookup(request.form.get("symbol"))
if quote == None:
return apology("invalid symbol", 400)
try:
shares = int(request.form.get("shares"))
except:
return apology("shares must be a positive integer", 400)
if shares <= 0:
return apology("can't buy less than or 0 shares", 400)
users= db.execute("SELECT cash FROM users WHERE id= :user_id", user_id=session["user_id"])
cash_remaining = users[0]["cash"]
price_per_share = quote["price"]
total_price = price_per_share * shares
balance = cash_remaining-total_price
symbol=quote["symbol"]
return render_template ("confirmation.html", cash_remaining=cash_remaining, price_per_share=price_per_share, total_price=total_price,shares=shares, symbol=symbol, balance=balance)
else:
return render_template ("buy.html")
#app.route("/confirmation", methods=["GET", "POST"])
#login_required
def confirmation():
if request.method == "POST":
db.execute("UPDATE users SET cash = cash = :balance WHERE id = :user_id", balance=balance, user_id=session["user_id"])
db.execute("INSERT INTO transactions (user_id, symbol, shares, price_per_share) VALUES(:user_id, :symbol, :shares, :price_per_share)",
user_id=session["user_id"],
symbol=request.form.get("symbol"),
shares=shares,
price_per_share=price_per_share)
flash ("Bought!")
return render_template("index.html")
else:
return render_template("confirmation.html")
here is my buy.html code in case needed:
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/buy" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Symbol" type="text" required/>
</div>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares Wanted" type="number" min="1" required />
</div>
<button class="btn btn-primary" type="submit">Get Price</button>
</form>
{% endblock %}
and finally my confirmation.html:
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/confirmation" method="post">
<p>Stock Symbol ={{symbol}}</p>
<p>Shares to be purchased = {{shares}}</p>
<p>Total Transaction Cost {{total_price | usd}}</p>
<p>Current Cash = {{cash_remaining | usd}}</p>
<p>Balance after transaction = {{balance |usd}}</p>
<button class="btn btn-primary" type="submit">Buy</button>
</form>
{% endblock %}
There are no values submitted with the form in confirmation.html. Some options:
add the data to the session array
create hidden input controls to carry the data back to the server
give the button element a name, and a json encoded string of the values that confirmation will need as its value.
The confirmation route doesn't (try to) get any values from the form (eg request.form.get).

HTML form only sends part of option value back to Flask

I'm getting a bad value when I return this option. For example, when i send n = "Arnold, Robert | ID: 1" as an option, it shows up on the web page, but when the user clicks submit, only "Arnold," is sent to my Flask Server. I'm trying to get the ID value out.
Does anyone know how to retrieve the entire option value from the form or another way to get the ID out of the string?
Here's the HTML
<h2>Donors</h2>
<form method="POST" action="/display_donor">
<select name="selected_donor">
{% for n in donors %}
<option value={{n}}>{{n}}</option>
{% endfor %}
</select>
<br />
<br><input type="submit" value="View donor"/>
</form>
Here's the relevant Flask function
#app.route('/display_donor', methods=['POST'])
def display_donor():
temp_id = request.form['selected_donor']
print "temp_id"
print temp_id
final_id = re.sub('[^0-9]','', temp_id)
print "final_id"
print final_id
display_value = 1
cursor = g.conn.execute("SELECT name,donor_id FROM donors")
donors = []
for result in cursor:
temp_string = result['name'] + " | ID: " +result['donor_id']
donors.append(temp_string)
# can also be accessed using result[0]
cursor.close()
print "this"
print final_id
#Returns a specific instance of transaction as specified by user
cursor = g.conn.execute("SELECT * FROM donors D WHERE D.donor_id='%s'" % final_id)
values = []
for result in cursor:
values.append(result['donor_id'])
values.append(result['name'])
values.append(result['donor_type'])
cursor.close()
#denote attribute names
attribute = []
attribute.append('donor_id')
attribute.append('name')
attribute.append('donor_type')
return render_template("donors.html",attribute=attribute,values=values,donors=donors,display_value=display_value)
I fixed this issue by sending the values separately rather than as a single string. This got me out of all the parsing and allowed me to print out the values like so:
<h2>Donors</h2>
<form method="POST" action="/display_donor">
<select name="selected_donor">
{% for n in donor_ids %}
<option value={{n}}>{{ donor_names[loop.index0] }} | ID: {{n}}</option>
{% endfor %}
</select>
<br />
<br><input type="submit" value="View donor"/>
</form>

Python. Django1.7 DoesNotExist. Matching query does not exist

I'm a learner in Python and Django.
I am trying to pass selected checkboxes to my views and then make a get() call to fetch related objects, but keep getting the error DoesNotExist, even though the object is present in the database.
I tried changing the get() parameters but it still shows the same error, as if it just cannot fetch the database. Please help!
ERROR IS IN THE #ed LINE
Here is my views.py
def manage(request):
if request.POST.get('taskname'):
name = request.POST.get('taskname')
end = request.POST.get('enddate')
emp = User.objects.get(username = request.user.username)
print emp.username
try:
newtask = Task(taskname = name, deadline = end, status = 'incomplete', empid = emp)
newtask.save()
except:
print "not saved"
my_tasks = Task.objects.filter(empid = emp)
return render(request, 'ellipse/dashboard.html', {'employee': emp, 'tasks': my_tasks})
else:
selected = request.POST.get('dropdown')
if selected == 'Delete':
tasks = request.POST.getlist('t')
emp = User.objects.get(username = request.user.username)
for seltask in tasks:
#deltask = Task.objects.get(taskname=seltask)
deltask.delete()
my_tasks = Task.objects.filter(empid = emp)
return render(request, 'ellipse/dashboard.html', {'employee': emp, 'tasks': my_tasks})
And, my html:
<div>
<form action="/ellipse/manage/" method="post">
{% csrf_token %}
<p>Taskname <input type="text" name="taskname"></p>
<p>Deadline <input type="date" name="enddate"></p>
<select name="dropdown">
<option selected="selected" disabled>Select action :</option>
<option value="Add">Add</option>
<option value="Delete">Delete</option>
<option value="Mark as complete">Mark as complete</option>
<option value="Mark as incomplete">Mark as incomplete</option>
</select>
{% if tasks %}
{% for tasko in tasks %}
<p><tr><td><input type="checkbox" name="t" value={{ tasko.taskname }}/></td><td>{{ tasko.taskname }}</td><td>{{ tasko.deadline }}</td><td>{{ tasko.status }}</td></tr></p>
{% endfor %}
{% endif %}
<p><button type="submit" name="modify">Modify</button></p>
</form>
</div>
I am clueless on how to proceed further and it'd be great help if this issue can be resolved. Thanks in advance!
Well your get looks jacked up.
emp = User.objects.get(username = request.user.username)
should probably be something like this.
emp = User.objects.get(id=request.user.id)
You could probably do this to...
emp = User.objects.get(user=request.user)

dynamically connecting Django Q objects with AND and OR

I want users to be able to query my database via several different parameters (name, year, etc), dynamically add more fields, and join them with boolean operators; the net result would be something like "year = 1900 AND name = chicken AND location = San Francisco." I think I'm doing something wrong, since it's not returning anything, even when I try just one field with a value that I know matches some data (e.g., I can get objects back when I use .filter() from the Django shell). Anyone know how I can fix it?
Relevant view (ignore the sloppy indentation, I didn't want to go through and fix all of it, but it's right in my actual code):
class BaseSearchFormSet(BaseFormSet):
def clean(self):
if any(self.errors):
return self.errors
queries = []
valid_courses = ["appetizer","main","dessert"]
valid_period = re.compile(r'\d\d\d0-\d\d\d5|\d\d\d5-\d\d\d0')
valid_year = re.compile(r'\d{4}')
multi_rows = ["year","period","course"]
for x in xrange(0,self.total_form_count()):
form = self.forms[x]
query = form.cleaned_data.get("query")
row = form.cleaned_data.get("row")
if query in queries and row not in multi_rows:
raise forms.ValidationError("You're already searching for %s.")
queries.append(query)
if row == "course" and query.lower() not in valid_courses:
raise forms.ValidationError("%s is not a valid course option."%(form.cleaned_data["query"]))
if row == "period" and not re.match(valid_period,query):
raise forms.ValidationError("%s is not a properly formatted period. Valid five-year periods span either the first or second half of a decade. For example: 1910-1915, 1925-1930."%(form.cleaned_data["query"]))
if row == "year" and not re.match(valid_year,query):
raise forms.ValidationError("Please enter a four-digit year.")
def search(request):
errors = []
searchFormSet = formset_factory(F.SearchForm, extra=1,formset=BaseSearchFormSet)
if request.GET:
formset = searchFormSet(request.GET)
forms = []
if formset.is_valid():
for x in xrange(0,formset.total_form_count()):
form = {}
form["row"]= formset[x].cleaned_data.get("row",None)
form["query"] = formset[x].cleaned_data.get("query",None)
form["bools"] = formset[x].cleaned_data.get("bools",None)
if form["query"]:
q = form["query"]
else:
errors.append("no query found")
if form["row"]:
row = form["row"]
else:
errors.append("no row found")
filter_keys = {"dish_name":Q(dish__name__icontains=q),
"regex":Q(dish__full_name__regex=r'%s'%(q)),
"course":Q(dish__classification=q.lower()),
"year":Q(page__menu_id__year__exact=q),
"period":Q(page__menu_id__period__exact=q),
"location":Q(page__menu_id__location__icontains=q),
"restaurant":Q(page__menu_id__restaurant__icontains=q)}
forms.append(form)
final_query=Q()
def var_reduce(op,slice):
if op == "and":
return reduce(lambda x,y: x & y,slice)
elif op == "or":
return reduce(lambda x,y: x | y,slice)
for x in xrange(len(forms)):
try:
try:
if final_query:
slice = [final_query,filter_keys[forms[x]["row"]],filter_keys[forms[x+1]["row"]]]
else:
slice = [filter_keys[forms[x]["row"]],filter_keys[forms[x+1]["row"]]]
final_query = var_reduce(forms[x]["bools"],slice)
except IndexError:
if final_query:
slice = [final_query,filter_keys[forms[x]["row"]]]
else:
slice = [filter_keys[forms[x]["row"]]]
final_query = var_reduce(forms[x]["bools"],slice)
items = MenuItem.objects.filter(final_query)
return render_to_response("search_results.html",{"items":items,"formset":formset})
except KeyError as e:
errors.append(e)
formset = searchFormSet(None)
return render_to_response("search_page.html",{"errors":errors,"formset":formset})
else:
formset = searchFormSet(None)
return render_to_response("search_page.html",{"errors":errors,"formset":formset})
else:
formset = searchFormSet(None)
return render_to_response("search_page.html",{"formset":formset})
models:
from django.db import models
class MenuItem(models.Model):
def format_price(self):
return "${0:0<4,.2f}".format(float(self.price))
def __unicode__(self):
return self.dish
dish=models.OneToOneField('Dish',to_field='mk')
price=models.CharField(max_length=5,blank=True)
page=models.OneToOneField('MenuPage')
mk=models.CharField(max_length=10,unique=True)
formatted_price = property(format_price)
class Menu(models.Model):
def period(self):#adapted from http://stackoverflow.com/questions/2272149/round-to-5or-other-number-in-python
try:
p=int(10*round(float(int(self.year))/10))
if p < self.year:
return "%s-%s"%(p,p+5)
else:
return "%s-%s"%(p-5,p)
except (ValueError,TypeError):
return ""
def __unicode__(self):
if self.restaurant:
return self.restaurant
else:
return self.mk
restaurant=models.TextField(unique=False,blank=True,null=True)
year=models.CharField(max_length=4,unique=False,blank=True,null=True)
location=models.TextField(unique=False,blank=True,null=True)
status=models.CharField(unique=False,max_length=20)
mk=models.CharField(max_length=8,unique=True,primary_key=True)
period=property(period)
language = models.CharField(unique=False,max_length=30)
#objects=MenuManager()
class MenuPage(models.Model):
mk=models.CharField(max_length=10,unique=True)
menu_id=models.OneToOneField("Menu",to_field='mk')
#objects=MenuPageManager()
class Dish(models.Model):
def __unicode__(self):
return self.name
full_name = models.TextField()
name=models.CharField(unique=True,max_length=255)
mk=models.CharField(max_length=10,unique=True)
class Classification(models.Model):
def __unicode__(self):
if self.classification:
return self.classification
else:
return "none"
dish=models.OneToOneField('dish',to_field='name')
classification=models.CharField(unique=False,max_length=9)
mk=models.CharField(max_length=10,primary_key=True)
html of my search page:
{% extends "base.html" %}
{% block style %}
<link rel="stylesheet" type="text/css" href="/static/search_style.css" />
{% endblock %}
{% block java %}
<script type="text/javascript" src="/static/searches.js"></script>
{% endblock %}
{% block title %}Search{% endblock %}
{% block head %}Search{% endblock %}
{% block content %}
{% autoescape off %}
<div id="searches">
<form id="search" action="" method="get">
<table border="0" cellpadding="0" cellspace="0">
<tbody class="search">
{% for form in formset.forms %}
<tr>
<td class="row">{{ form.row }}</td>
<td class="query">{{ form.query }}</td>
<td class="bool">{{ form.bools }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ formset.management_form }}
<input type="submit" value="Submit" id="submit">
</form>
</div>
{% if formset.errors or errors %}
<div id="errors">
<h3>The following errors were encountered while trying to submit your search:</h3>
{% for x,y in formset.errors.items %}
<p>{{ x }} : {{ y }}</p>
{% endfor %}
{{ errors }}
</div>
{% endif %}
<div id="notes">
<p>Searching by dish names, locations, and restaurants is case-insensitive.</p>
<p>Searching by course uses case-insensitive exact matching. Valid courses are Appetizer, Main, and Dessert.</p>
<p>Years should be entered YYYY. Five-year periods span either the first or second half of a decade, and should be entered YYYY-YYYY. Example valid five-year periods are 1900-1905, 1995-2000, etc.</p>
<p>Regular expression search follows MySQL regular expression syntax, as described here.</p>
</div>
{% endautoescape %}
<br /><br /><br /><br /> <br /><br /><br />
{% endblock %}
{% block footer %}
<div id="warning">
<p>NOTE: This site and the information it contains are still in development. Some information may be missing or inaccurate.</p>
</div>
<div class="credits">
Created and maintained by Sam Raker and Rachel Rakov
<br />
Data graciously provided by What's on the Menu?
</div>
{% endblock %}
Sorry, my original post was more than you needed. To clarify, all you should need to do is:
Q(year__icontains=year_input_variable) | Q(city__icontains=city_input_variable) & Q(name__icontains=name_input_variable)
Use & for and, | for or.
What I had posted earlier is for if a query contains multiple words, it would either check to see if all of the words matched using operator.and or if any of the words matched using operator.or.

Categories

Resources