Getting 'GET' parameter in views.py - python

I am trying to get a 'GET' parameter from one of my template pages, called 'id' (ex. issues.html?id=1), and using it to perform a variety of retrievals from my sqlite database. However, when I run the following code, I get the error "'QueryDict' object has no attribute 'id'"
Here is some of my code in views.py:
def issues(request):
# Retrive basic legislation details from GET parameter
legislation_id = request.GET.id
legislation = Legislation.objects.filter(id = legislation_id)[0]
# Getter functions from database - names are pretty self-explanatory
def get_buttons():
button_html = ""
for tag in legislation.demographic_tags:
button_html += "<input type='button' class='button' onclick='showtag"+tag.hash_code+"();' value='"+tag.name+"'>"
return button_html
def get_description():
return legislation.description
def get_script():
return ""
def get_sponsor():
return legislation.sponsor
def get_summary():
return legislation.summary
def get_title():
return legislation.name
# Return results back to page
return render(request, 'issues.html',
{'buttons': get_buttons(), 'description': get_description(), 'script': get_script(), 'sponsor': get_sponsor(),
'summary': get_summary(), 'title': get_title()})

Your problem seems to be here: legislation_id = request.GET.id
request.GET is a dictionary so you need to get its key like this:
legislation_id = request.GET["id"]
You can also get the key with dict's get method as #Alvaro suggested: request.GET.get("id", 0). This will return 0 as default if id key ain't present and save you from KeyError exception.
Hope this helps!

Related

added two search bar in django one search by id and other by name .Tried many ways but cannot do it if i try by id then i am not able to do with name

I have to make 2 search bars. I don't know how to add multiple fiels...
match= Staff.objects.filter(id=srch1...) here how can I add name=srch1
over here after trying many ways I found it but the problem is all input here is string how to change it to int
def search(request):
# Catch the data and search in Staff model.
if request.method=='POST':
srch1 = request.POST['srch']
print(type(srch1))
if type(srch1)== int:
match= Staff.objects.filter(id=srch1)
if match :
return render(request,'search.html',{'sr': match})
else:
messages.error(request,'no results,found')
elif type(srch1)== str:
catch= Staff.objects.filter(name=srch1)
if catch:
return render(request,'search.html',{'sr': catch})
else:
messages.error(request,'no results,found')
else:
return HttpResponseRedirect("/search")
return render(request,"search.html")
You should be using a GET request with querysting parameters or (url parameters, which is a bit more complicated) for this, not a POST. Here's how I would do this:
def search(request, *args, **kwargs):
# Initial empty query dictionary for use with query later.
query = {}
# Getting 'name' and 'id' from querystring parameters (i.e. ?id=1&name=foo)
name = request.GET.get('name', None)
id = request.GET.get('id', None)
# Add 'id' to the query dictionary if it exists
if id is not None:
query['id'] = id
# Add name to the query dictionary if it exists
if name is not None:
query['name'] = name
# If the query dictionary has name or id, get the Staff entry from the database
if query.get('name', None) or query.get('id', None):
# Note that .filter() returns a QuerySet. You should probably use .get()
# since my guess is that you only want one Staff object (judging by your
# search parameters). Also note that since we are using **query we will be
# using BOTH 'name' AND 'id' to search for the Staff, as long as both exist in
# the query dictionary.
match = Staff.objects.get(**query)
# If there is a match, send it in the rendered response context dict
if match:
return render(request, 'search.html', {'sr': match})
# no match, send message notifying that a Staff entry was not found matching
# the desired criteria
return render(request, 'search.html', {message: 'Not Found'}
# There were no query parameters, so we are not searching for anything.
else:
return render(request, 'search.html')
You can see that the above code is much simpler, and more concise. This will help you or anyone else checking out your code in the future to better understand what you're trying to acheive.
P.S. I typically only use POST requests when I am creating an entry in the database. Maybe this is preference, but to my knowledge it is best practice to use a GET request for search.

How to create a custom generic view from a web2py RESTFUL service?

Using Web2Py RESTFUL services, I'd like the following html to render in the browser after navigating to this link (http://127.0.0.1:8000/app/default/api/example_data.html_table/?limit=1):
<div><table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE</td><td>Smith</td><td>1</td></tr></tbody></table></div>
However, I currently receive the following:
<div><table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>SUUUUPPPEEEER...</td><td>Smith</td><td>1</td></tr></tbody></table></div>
The difference is that Web2Py is shortening "SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE" to "SUUUUPPPEEEER..." but I need the entire text
My View called generic.html_table that generates this is the following:
{{=BEAUTIFY(response._vars[next(iter(response._vars))])}}
Controller
#request.restful()
def api():
response.view = 'generic.' + request.extension
def GET(*args,**vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns,args,vars)
return dict(content=parser.response)
def POST(table_name,**vars):
if 'id' in vars.keys():
return db[table_name].update_or_insert(db[table_name]._id == vars['id'],**vars)
else:
return db[table_name].validate_and_insert(**vars)
def PUT(table_name,**vars):
record_id = vars['id']
return db(db[table_name]._id==record_id).update(**vars)
def DELETE(table_name,record_id):
return db(db[table_name]._id==record_id).delete()
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)
MODEL
db.define_table('example_data', Field('Firstname', 'string'),Field('Lastname', 'string'),Field('Age', 'integer'))
I've also tried the following views:
{{=response._vars[next(iter(response._vars))]}}
RESULT (first name still cut off):
<table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>Jill</td><td>Smith</td><td>1</td></tr><tr class="w2p_even even"><td>2</td><td>Eve</td><td>Jackson</td><td>33</td></tr><tr class="w2p_odd odd"><td>3</td><td>afdaskfdlasjf...</td><td>Jackson</td><td>33</td></tr><tr class="w2p_even even"><td>4</td><td>SUUUUPPPEEEER...</td><td>Jackson</td><td>33</td></tr></tbody></table>
I've also tried the following views:
{{=XML(response._vars[next(iter(response._vars))])}}
RESULT (Lost all HTML formatting):
example_data.id,example_data.Firstname,example_data.Lastname,example_data.Age
1,Jill,Smith,1
2,Eve,Jackson,33
3,afdaskfdlasjfkdlsjfklajdfskasjfklsdajfdklsajfklsajfdskalfdjsakldfjklasfjkdlsajfdsakljdklsadcjklasjcklsjackldsjakfldajsfklasdfjklasjfdklajfdsklsjafkldasjfkldasjkldsjcklajsckljackldajsdfklfjkalsncklacnkalsdfjkldasnckldasjckljsdaklfdnfkldsajfdklasjldsk,Jackson,33
4,SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE,Jackson,33
All of those methods ultimately result in the DAL Rows object being passed to SQLTABLE for rendering in the view. By default, fields are truncated to 16 characters. If you want to change that, you will have to call SQLTABLE explicitly:
{{=SQLTABLE(content, truncate=100)}}

How to use a string variable as a fieldname when looking up a model object using filter / the Django ORM?

This is my code:
PostModels = {
'dPost': DPost,
'sPost': SPost,
'rPost': RPostModel,
}
for postType, Post in PostModels.items():
try:
post = Post.objects.get(pk=pk)
except:
pass
else:
return RPostModel.objects.filter(postType=post)
When I do post = Post.objects.get(pk=pk), Post is the variable Post in the for loop. So on first loop, it searches the objects in DPost, on the next loop it searchs SPost etc. However, when it reaches the return statement, it gives an error saying:
django.core.exceptions.FieldError:
Cannot resolve keyword 'postType' into field.
Choices are: createdAt, dPost, dPost_id, sPost, sPost_id, rPost, rPost_id
How do I make postType equal to what the postType variable is equal to? In other words, in the loop, how can I get my return statement to be these statements:
return RPostModel.objects.filter(dPost=post)
return RPostModel.objects.filter(sPost=post)
return RPostModel.objects.filter(rPost=post)
rather than this:
return RPostModel.objects.filter(postType=post)
Try this:
for postType, Post in PostModels.items():
try:
post = Post.objects.get(pk=pk)
except:
pass
else:
kwargs = {postType: post}
return RPostModel.objects.filter(**kwargs)

How can I return a list of results as JSON?

I want to return the result of a query as JSON. I'm using the following route to return one model instance as a JSON object.
#mod.route('/autocomplete/<term>', methods=['GET'])
def autocomplete(term):
country = Country.query.filter(Country.name_pt.ilike('%'+ term + '%')).first()
country_dict = country.__dict__
country_dict.pop('_sa_instance_state', None)
return jsonify(json_list=country_dict)
This code works well if I use the first() method. However, I need to use the all() to get all results. When I do that, I get the following error.
country_dict = country.__dict__
AttributeError: 'list' object has no attribute '__dict__'
What should I be doing to return the entire list of results as JSON?
You need to do that "jsonify preparation step" for each item in the list, since .all() returns a list of model instances, not just one instance like .first(). Work on a copy of each __dict__ so you don't mess with SQLAlchemy's internal representation of the instances.
#mod.route('/autocomplete/<term>', methods=['GET'])
def autocomplete(term):
countries = []
for country in Country.query.filter(Country.name_pt.ilike('%' + term + '%'):
country_dict = country.__dict__.copy()
country_dict.pop('_sa_instance_state', None)
countries.append(country_dict)
return jsonify(json_list=countries)
Probably better just to return the data about each country explicitly, rather than trying to magically jsonify the instance.
#mod.route('/autocomplete/<term>', methods=['GET'])
def autocomplete(term):
countries = []
for country in Country.query.filter(Country.name_pt.ilike('%' + term + '%'):
countries.append({
'id': country.id,
'name': country.name_pt,
})
return jsonify(countries=countries)

pass in table name to sqlalchemy query through url in flask

In short, how do i
var="TableName"
models.var.query.all()
explanation
My goal is to allow the user to change the order of list of items.
I set up an ajax call that sends an array of id's to the api below.
It works if i hard code the query, and make a api view per table.
my problem is that i want "table" to fill in this line
models.table.query.filter_by(id=item).first()
to complete the query.
here is the view api which gives me an error "no attribute 'table'"
#app.route('/order/<table>')
def order(table):
# import pdb;pdb.set_trace()
sortedItems = request.args.listvalues()[0]
o=1
import pdb;pdb.set_trace()
for item in sortedItems:
grab = models.table.query.filter_by(id=item).first()
grab.order=o
o=o+1
db.session.commit()
return jsonify(result=sortedItems)
You can use getattr():
>>> var = 'table_name'
>>> table = getattr(models, var)
>>> table.query.filter_by(id=item).first()
getattr() will raise an AttributeError if the attribute your trying to get does not exist.
Example for your order()-function:
#app.route('/order/<table>')
def order(table):
sortedItems = request.args.listvalues()[0]
o=1
table = getattr(models, table)
for item in sortedItems:
grab = table.query.filter_by(id=item).first()
grab.order=o
o=o+1
db.session.commit()
return jsonify(result=sortedItems)

Categories

Resources