Passing a Tuple of Values through a Checkbox - python

I have a list of organized items that users may choose from using checkboxes. I am trying to pass a tuple of values for each checked checkbox so I can get information about the item itself and the group to which the item belongs. I have tried using hidden fields but it seems that the hidden field value is being passed regardless of if the corresponding checkbox has been checked or not.
If a checkbox is checked, I need the citation id and the parent software. Can I pass a tuple of (citation.id, sw) for each checked checkbox and, because multiple checkboxes can be checked, pass all of these together as a list of tuple? Like: [(citation1.id, sw1), (citation2.id, sw2), ] ? I need this info in my view.
Thank you for any help!
select_citations.html
{% for sw in software %}
{{sw}}
{% for citation in all_citations %}
<input type="checkbox" name="myselection[]" value="{{citation.id}}">
<input type="hidden" name="myselection[]" value="{{sw}}">
{% endfor %}
{% endfor %}

Compose IDs of both models to a single value for the checkbox:
{% for sw in software %}
{{sw}}
{% for citation in all_citations %}
<input type="checkbox" name="selection" value="{{citation.id}}-{{sw.id}}">
{% endfor %}
{% endfor %}
And then deconstruct this values in the view:
ids = [value.split('-') for value in request.POST.getlist('selection')]

Related

Sort dropdown alphabetically after using jinja2 to build it from a list with for loop & replacing one item with if statement? (Flask app)

I have a python list of files, results, that was created in my flask app.
I create a dropdown select list in my HTML templates by looping through this list. Each file in the list becomes an option with the value and text being the file name. However, I use an if statement to find the query_results.csv file and change the option text to "All Results" instead of using the file name.
<select name="result" method="GET" action="/">
{% for result in results %}
{% if result == 'query_results.csv' %}
<option value="{{ result }}">All Results</option>
{% else %}
<option value="{{ result }}">{{ result }}</option>
{% endif %}
{% endfor %}
</select>
How can I order this select menu alphabetically after I've done this, so that All Results appears at the top where expected? Should I have replaced the item in the list in python and sorted first before building the select menu?

Is there a way to dynamically set the default value of a wtform radiofield from within the html template?

I'm trying to dynamically set the value of a radio field within the HTML template but I'm not sure how to set the default selection. I want to do this because the forms I want to use are forms that can be saved and re-edited if need be, so I want the default values to be values set when previously saved/submitted.
the wtform fields
field_1 = StringField('field_1')
radio_1 = RadioField('radio_1', choices=[(1,'Yes'),(2,'No')])
what i want to be able to do is something like this:
data is database data
{% if data.field_1 = 'X' %}
{{ form.radio_1(id="radio_1",class="ff-style-radio",default=1) }}
{% else %}
{{ form.radio_1(id="radio_1",class="ff-style-radio",default=2) }}
{% endif %}
I haven't had any success trying this method, swapping default for value, etc. Is something like this possible? If not how would I separate the wtform radio field choices so I can just manually mark which selection is checked? Or should I just use the base HTML method and do something like this:
<ul class="ff-style-radio" id="radio_1">
<li>
{% if data.field_1 = 'X' %}
<input id="radio_1-0" name="radio_1" type="radio" value="Yes" checked>
{% else %}
<input id="radio_1-0" name="radio_1" type="radio" value="Yes">
{% endif %}
<label for="radio_1-0">Yes</label>
</li>
<li>
{% if data.field_1 = 'X' %}
<input id="radio_1-1" name="radio_1" type="radio" value="No">
{% else %}
<input id="radio_1-1" name="radio_1" type="radio" value="No" checked>
{% endif %}
<label for="radio_1-1">No</label>
</li>
</ul>
Thanks for any help
If you want to use the form to edit data already in your database, you should supply this data to the form at creation time in your view function. This will allow you to keep your templates simple and reusable. Data from the database can be passed to the form either as an object or as a dictionary https://wtforms.readthedocs.io/en/stable/forms.html. If your form maps directly to a database table you can just pass this unmodified from your query. In the following code I've followed your example and created a new dictionary to set the value of radio_1 based on the value of field_1.
#app.route("/myurl")
def myview():
# do database lookup here
olddata = {"radio_1": 1 if data.field_1 == "X" else 2}
form = Myform(formdata=request.form, data=olddata)
if request.method == "POST" and form.validate():
# .....

Django - Passing multichoice field into POST dictionary

I have a queryset generated in my forms.py file that passes into my template. The template result is a multichoice field based on the queryset. The web browser presentation is correct - it renders the queryset as a drop down choice list that I can make a selection from.
Here is the template code:
<tr><td>{{ form.jury_name | placeholder:'Jury Name' }}</td></tr>
<tr><td><select>
{% for item in form.parent_jury.field.queryset %}
<option name="parent_jury" value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
This is all contained in a table.
When the form is submitted (method = "POST") the POST dictionary has all the correct values for the keys except the parent_jury key which posts a value of ''.
I've worked through several SO solutions on the views.py side, but they don't change the fact that the information available for a clean() is missing the choice field value for 'parent_jury'. How do I get the selected option from the list to attach to the 'parent_jury' key?
I think your rendered HTML is not the way it is supposed to be: the name="..." should be part of the <select> tag, not the <option>s:
<tr><td>{{ form.jury_name | placeholder:'Jury Name' }}</td></tr>
<tr><td><select name="parent_jury">
{% for item in form.parent_jury.field.queryset %}
<!-- remove the name here -->
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
(of course you can remove the <!-- comment --> part (this is only meant to draw attention to this change).

Retrieve clicked list item in Django

I'm using a list to populate / generate some html code dynamically as shown below,
<ul>
{% if results %}
{% for result in results %}
<li><a href="/accounts/evalpage/" ><strong>{{result.0}}</strong>{{result.1}}</a></li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
I'm running into an issue with that if one of the list items is clicked, i need to be able to retrieve the information stored in that list item, e.g if item one is clicked i need to retrieve {{result.0}} and {{result.1}} is there a way i can retrieve this information?
List is shown below :
[['User1', 'InProgress'], ['User2'], ['User3'], ['User3'], ['User4']]
For instance, if the row containing User1 and InProgress is clicked by the end user, i want to be able to have the information User1 and InProgress within Django such that operations can be performed with them
It should be clear that in order for the backend to know what object you clicked on, you need to pass that value in the URL. So, you'll need to change the definition of your "evalpage" URL to accept a parameter:
url(r'^accounts/evalpage/(?P<user>\w+)/$', views.evalpage, name='evalpage')
and the view signature:
def evalpage(request, user):
...
and now you can do:
...
try this:
<ul>
{% if results %}
{% for result in results %}
<li>
<a href="/accounts/evalpage/" >
{% for item in result %}
<strong>{{item}}</strong>
{% endfor %}
</a>
</li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>

Django multiselect checkboxes

I have a list of objects, each with it's own checkbox, where the user can select multiple of these. The list is a result of a query.
How can I mark in the view which checkboxes are already selected? There doesn't seem to be an in operator in the template language.
I want something along the lines of:
<input {% if id in selectedIds %}checked {% endif %}>
You could use a templatetag like the one in this snippet comments:
http://www.djangosnippets.org/snippets/177/
#register.filter
def in_list(value,arg):
return value in arg
To be used in templates:
The item is
{% if item|in_list:list %}
in list
{% else %}
not in list
{% endif %}
Not very smart, but it works.

Categories

Resources