i need a small help.. i am looping over objects which are in db and rendering all objects to template, so far so good. but what i want is that every rendered html name field should have different name so that i can refer to all of them latter. my code is this:
{% for p in products %}
<input type="number" name="name1" value="{{p.arg1}}" size="12"/>
<input type="number" name="name2" value="{{p-arg2}}" size="12"/>
{% endfor %}
but if i have 4 objects in DB, then i will have 8 rendered input fields in template, but all of them will have the "name" value as name1 and name2, is it possible to create 8 different names dynamically so that i can refer to all of them in my view again...
thanks
Use the forloop.counter variable
{% for p in products %}
<input type="number" name="name-{{forloop.counter}}-arg1" value="{{p.arg1}}" size="12"/>
<input type="number" name="name-{{forloop.counter}}-arg2" value="{{p.arg2}}" size="12"/>
{% endfor %}
The forloop.counter is 1-indexed. There is also the forloop.counter0 for indices
starting with 0.
Are you sure though that django formsets isn't what you need?
Related
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():
# .....
I've made the sudoku generator and the templates, but now I stuck in posting the whole array(9x9) to Django (i.e. sudoku_checker) for checking the duplicates and determine whether the user can go next game.
Here is my template looks like in Django, and you'll see I've indexed/positioned every single table cell, thought it might help with later checking:
<table>
<caption>Sudoku of the day</caption>
{% for row in numbers %}
<tr>
{% for col in row %}
{% if col is 0 %}
<td>
<input class="d0" size="1" autocomplete="off" maxlength="1" name="{{ forloop.parentloop.counter0 }}{{ forloop.counter0 }}">
</td>
{% else %}
<td id="{{ forloop.parentloop.counter0 }}{{ forloop.counter0 }}">{{ col }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
<button class="btn btn-primary" type="submit">Submit</button>
but then what should I do next? Am I correct to use Form method to Post the data to Django? But how can i make sure all the data have been bundled up when passing through for checking, in terms of both the known numbers and unknown numbers(input by users)? any hints please?
This is the code of the views, but i only completed the numbers_fill_table one, i dont know what to write in second part of views and forms properly to post the exact data i want. Please help!
from .sudoku_generator import sudoku_generator
def board_fill(request):
context = {'numbers': sudoku_generator.final_board()}
template = 'sudoku_board.html'
return render(request, template, context)
When you have the view (you must map it to the url that is used as the form target) it gets the request object. From there you can read the user input:
request.POST.get('44', 0) # Will return 0 if 44 is not found
More hint about writing a view: https://docs.djangoproject.com/en/2.1/topics/http/views/ and mapping the view to the url: https://docs.djangoproject.com/en/2.1/topics/http/urls/
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')]
I'm trying to set maxlength html attribute in django template but when I try to get the value I specified in my model by using {{field.max_length}} nothing is displayed. I can get any other field value just fine.
Relevant Snippet:
{% for field in form %}
{% if "_color" in field.name %}
<div class="control-group">
<label class="control-label">{{field.label}}</label>
<input type="text" ng-init="{{field.name}}='{{field.value}}'" ng-model="{{field.name}}" ng-change="{{field.name}}_picker={{field.name}}" placeholder="{{field.label}}" maxlength="{{field.max_length}}">
The field you get by iterating trough your form is actually a forms.BoundField, and does not have all the attributes of your Field. You can access the Field instance through the field attribute of your BoundField. So, {{ field.field.max_length }} should work.
All,
How Can we increment a value like the following in django templates,
{{ flag =0 }}
{% for op in options %}
{{op.choices}}<input type="radio" name="template" id="template" value="template{{flag++}}"/>
{% endfor %}
thanks..
I don't think it's intended you should alter data in your templates. For in your specific case, you could instead use the forloop.counter variable.
For example:
{% for op in options %}
{{op.choices}}<input type="radio" name="template" id="template{{forloop.counter}}" value="template{{forloop.counter}}"/>
{% endfor %}
Also note that I added that number to the id attributes of the <input /> tag. Otherwise you'll have multiple inputs with the same id.
EDIT: I didn't note that it was a radio input. You could of course have the same name for each <input type="radio" />.
You explicitly can't do that in a template. Variable assignment is not allowed.
However if all you want is a counter in your loop, you just need to use {{ forloop.counter }}.
You might also want to look into having Django forms produce these values