pyramid checkboxes - python

I'm just new to python and pyramid and I'm struggling with how to process the results of a form containing multiple checkboxes in Pyramid.
Here is an excerpt from my form:
<p tal:repeat="category categories">
<input type="checkbox" name="selectedcategories" value="${category.id}"> ${category.name}<br/>
</p>
And here is how I am currently trying to iterate through and process the results:
selectedcategories=request.params['selectedcategories']
for categoryid in selectedcategories:
category = DBSession.query(Category).filter_by(id=categoryid).one()
article.categories.append(category)
As you may have guessed, I'm only getting a maximum of one checkbox recognized no matter how many I select on the form. Django has an option to return the results as a list, but I can't seem to figure out how to do that with Pyramid.

request.params is a multidict. To retrieve multiple values, you can call its getall() method:
selectedcategories = request.params.getall("selectedcategories")

Related

Showing complete value while you're typing in input form (html)(jinja2)

A bit like autocomplete, but instead of cookies, I'm trying to use this database called contacts to retrieve likely values and this database is managed by python/flask.
I'm trying to make it work like...
1) As you type 'bana'
2) It shows values passed from python (something like 'banana' and 'bananas') below the input form.
<form action='/sendmail' method='post'>
<input type='text' name='email' placeholder="To"
...
<input type='submit' value='send'>
</form>
Assuming the name of a passed list containing related values is likelyValue, how should I incorporate likelyValue into this html file? I use jinja2 template but I can adjust code once I know how original html deals with it.

Web2py AJAX value

I have a list of Projects in my view and I would like to dynamically generate individual pages for each one.
My view:
{{for i in project_list:}}
<ul>
<input id="hello" type="hidden" value="{{response.write(i)}}" name="project_Name">
<li><a onclick="ajax('{{=URL('default', 'view_project')}}', ['project_Name'], 'target');">View Project</a></li>
</ul>
{{pass}}
My controller:
def view_project():
print request.vars.project_Name
return dict(name=request.vars.project_Name)
Essentially, I would like to identify each Project by its project_Name.
This is the current output from the controller:
['Customizable logistical service-desk ', 'Extended contextually-based prod
uctivity ', 'Face-to-face modular circuit ', 'Multi-tiered stable intranet
', 'Quality-focused coherent budgetary management ']
Why am I receiving an Array of all project names as output?
I just want to identify each project.
Any advice is appreciated!
The web2py ajax function sends to the server the value of the html element with the name attribute passed in the functions second parameter ("project_Name" in your case), but if there are more than one field with the same name, it will send the values of all of them.
So, you are creating with a loop many fields with the same name, and the ajax function is sending all the values to the server.
You can solve the problem appending a count variable to each field name (and its name on his onclick attribute):
{{count = 1}}
{{for i in project_list:}}
<ul>
<input id="hello" type="hidden" value="{{response.write(i)}}" name="project_Name{{=count}}">
<li><a onclick="ajax('{{=URL('default', 'view_project')}}', ['project_Name{{=count}}'], 'target');">View Project</a></li>
</ul>
{{count += 1}}
{{pass}}
Or use a more clean solution usin the JQuery ajax function
You have:
ajax('{{=URL('default', 'view_project')}}', ['project_Name'], 'target')
You have specified "project_Name" as the form input element from which to extract the value to post. However, there are multiple inputs on the page all with that same name. As a result, the Ajax function serializes all of their values into a single list and posts the whole list.
You could give each input a unique name, but there is a simpler solution. It appears you are using the hidden input elements merely to hold values to be sent by the Ajax function. There is no need for that -- instead, you can simply encode the values directly in the Ajax URL. So, get rid of the input elements and change your ajax call to:
ajax('{{=URL('default', 'view_project', vars=dict(project_name=i)}}', [], 'target')

Generating random ID from list - jinja

I am trying to generate a random ID from a list of contacts (in Python, with jinja2) to display in an HTML template.
So I have a list of contacts, and for the moment I display all of them in a few cells in my HTML template by looping through the list of contacts:
# for contact_db in contact_dbs
<tr>
<td>{{contact_db.key.id()}}</td>
<td>{{contact_db.name}}</td>
<td>{{contact_db.phone}}</td>
<td>{{contact_db.email}}</td>
</tr>
# endfor
The view that renders the above is:
def contact_list():
contact_dbs, contact_cursor = model.Contact.get_dbs(
user_key=auth.current_user_key(),
)
return flask.render_template(
'contact_list.html',
html_class='contact-list',
title='Contacts',
contact_dbs=contact_dbs,
next_url=util.generate_next_url(contact_cursor),
)
Instead, I would like to display one contact, selected randomly by its ID, and it should display another contact with all its information every time the user refreshes the page (I am not dealing with displaying the same contact twice for now by the way).
I know that it is possible to use random in a python file to deal with random choices, so but not sure how it translates in jinja in the template.
Any help appreciated thanks!
There is a random filter in jinja2.
random(seq)
Return a random item from the sequence.
Use it like this:
{% set selected_contact = contact_dbs|random %}
note: I assumed contact_dbs is iterable.

For a checkbox with multiple values in my html template how to access it through python code?

I'm a python and django newbie.
This is in my html template
<input type ="checkbox" value={{ item.id }} name="ck1[]">
In views.py when i do a checked = request.POST.get(['ck1']) i get unhasable list error. Kindly guide me.
Please don't use PHP syntax when you're writing Django. name="ck1[]" is a PHP-ism that's completely unnecessary.
If you want the field to be called ck1, just call use name="ck1", and use `request.POST.getlist('ck1') in your view.
If you really have to use that horrible bracket syntax, you'll need to use request.POST.getlist('ck1[]'), because Django quite sensibly believes that the name you use in the HTML is the name you should get in the POST data.
If u want to get array from html in Django view u need to use
checked = request.POST.getlist('ck1[]')
or
checked = request.POST.getlist('ck1')
getlist method will convert all selected values into python list

What is the format when I get data from multiple checkboxs? Can I get a list?

I have a Kind Tag with property tagName, and in a html, I code like this
{% for tag in tags%}
<input type="checkbox" name="tagName" value={{tag.tagName}}>{{tag.tagName}}
{% endfor%}
Now, In my python file, I try to do this:
tagNames = self.request.get('tagName')
I want a list that contain every choice user choose. But what I got seems not.
So.
how can I get a list of user choices using request.get()?
Is there any other ways? I am not familiar with django, I just import gae's template. Can I use gae's template to simplify
this question?
Thank you!
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict
Looks like QueryDict.getlist(key, default) will get you what you want.
ie. self.request.getlist('tagName')
Assuming you're using the webapp framework, instead of self.request.get(), use self.request.get_all('tagName'), which will return a list of values.

Categories

Resources