i have the following piece of code:
<h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
{% for keyword in keyword_list %}
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
{{keyword.keyword_name}}
<br />
{% else %}
<input type="checkbox" name="cb" value="keywords" />
{{keyword.keyword_name}}
<br />
{% endif %}
{% endfor %}
Right now it just displays a checkbox of keywords one by one. I was wondering if there is anyway which i can turn this into a table form.
I need the table to be dynamic because the number of keywords in the list would keep on expanding.
I've tried to come up with some solutions with using the keyword id that is stored in the database but that's a tedious method.
Any other efficient method that i may have missed out?
This is a mixture of django and html so don't be alarmed by the unique terms. :p
Thanks for helping! :D
Why not just do it explicitely?
<h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
<table>
<tbody>
{% for keyword in keyword_list %}
<tr>
<td>
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
{% else %}
<input type="checkbox" name="cb" value="keywords" />
{% endif %}
{{keyword.keyword_name}}
</td>
</tr>
{% endfor %}
</tbody>
</table>
If you want the table to grow horizontally just move <tr></tr> tags outside the outer for.
For further deployment you may want to use Django Forms, with custom templates packed into a FormsSet.
EDIT:
If you want a N-column layout (3-columns for example), you can access a forloop.counter variable:
<h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
<table>
<tbody>
{% for keyword in keyword_list %}
{% if forloop.counter|divisibleby:"3" %}
<tr>
{% endif}
<td>
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
{% else %}
<input type="checkbox" name="cb" value="keywords" />
{% endif %}
{{keyword.keyword_name}}
</td>
{% if forloop.counter|add:"1"|divisibleby:"3" %}
</tr>
{% endif}
{% endfor %}
</tbody>
</table>
Code above works only when the length of a list of keywords is divisible by three, but it illustrates the general idea. To fix thath you can for example append empty items to the list, to make it satisfy that condition. A custom filter which do so may be a good idea.
If you want something more sophisticated it's again a play with if conditions and forloop variables.
For something very sophisticated it may be better to write a custom filter or a template tag, in order to make your template file clearer.
Related
#app.route('/',methods = ['POST'])
def user_rating():
for i in form_value['Rating']:
print(request.form.get(str(i)))
return("Thank You")
I tried various methods but they are not working. Is there any way to iterate over form name and get their value
Above is the code I am working on and want to get data from multiple forms with a single button. But able to get only the first value from the form.
HTML code:
<tbody>
{% for record in records %}
<tr>
{% for col in colnames %}
<td>
{% if (col != 'poster_link') and ( col != 'Rating' ) %}
{{ record[col] }}
{% endif %}
<!--Movie Poster-->
{% if col == 'poster_link' %}
<img src={{ record[col] }} style="width:150px">
{% endif %}
<!--end of Movie Poster-->
<!--form Rating-->
{% if col == 'Rating' %}
**<form method="post" id="FORMID" action="{{url_for('user_rating')}}">
<input type="text" name= {{ record[col] }} placeholder="Give Rating 1 to 5" >
</form>**
{% endif %}
<!--end of form Rating-->
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" form="FORMID" value="Submit">Submit</button>
Getting output like this
1
None
None
None
None
None
None
None
None
None
That is not possible because a user can only submit one form on a page. Whichever form is submitted is the one that gets read by your backend code. What you should do is either:
Use AJAX to make a request every time the input is changed, or
Only use one form
Based on your code, it appears that it will be easier to just use one form. So something like this would work:
<form method="post" id="FORMID" action="{{url_for('user_rating')}}">
<table>
<tbody>
{% for record in records %}
...
<input type="text" name= {{ record[col] }} placeholder="Give Rating 1 to 5" >
...
{% endfor %}
</tbody>
</table>
<button type="submit" form="FORMID" value="Submit">Submit</button>
</form>
my Question is: I want do a condition in Django Template with css but it didnt work.
<label>{% if not typ.required %}
{% trans 'Bitte Auswahl treffen' %}{% endif %}</label>
<input type="checkbox" {% if typ.required %} style="display:none;" checked="checked"
{% endif %} name="checkbox"
data-toggle="toggle" id="{{ typ.typ_id }}"
value="{{ cookie_id }}" />
{% if typ.required %} style="display:none;" checked="checked"{% endif %}
checked="checked" works but the Style not.
If i press f12 to watch the site i see this style in the input type
Thank you guys
i find the solution. The div above overwrite the style of the checkbox :-)
I have an array of checkboxes.
{% for groups in groupList %}
<tr>
<td id="checkboxes">
<input type="checkbox" name="check" id="check_{{groups.GroupID}}">
</td>
<tr>
{% endfor %}
I'd like to pass this array (preferably by ID so I can get the GroupID in the view) in a POST message. I'm not sure how to send this data though. Is there any way of attaching an array to a form parameter? Or any alternative solution?
{% for groups in groupList %}
<tr>
<td id="checkboxes">
<input type="checkbox" name="groups" id="check_{{groups.GroupID}}" value="{{groups.GroupID}}">
</td>
<tr>
{% endfor %}
In your views.py
groups=request.POST.getlist('groups')
I need to replicate the functioning of Flaskr example (the minimal blog application),
but instead of populating the fields of the output with the database contents I am getting those values from a text file in the disk.
I am not sure how to read values from a text file and print them, here is the code used to retrieve values from the DB:
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('main') }}" method=get,post class=main>
<dl>
<dt>Available:
<dd><input type=text size=15 name=available>
<dt>Used frequency:
<dd><input type=text size=15 name=used>
<dd><input type=submit value=Update>
</dl>
</form>
{% endif %}
<table border="1" style="width:300px">
<ul class=localDB>
<tr>
</ul>
<th></th>
<th>MyValues</th>
<th>maxValue</th>
</tr>
<tr>
<th>Available</th>
<td>{% for entry in localDB %} {{ entry.available }} {% endfor %}</td>
<td>N/A</td>
</tr>
<tr>
<th>Used</th>
<td>{% for entry in localDB %} {{ entry.used }} {% endfor %}</td>
<td>N/A</td>
</tr>
</ul>
</table>
{% endblock %}
Do I have to use other tools like JavaScript or I can use Flask and get the result I want.
here's how to read from a file and split the first and second lines.
file = open('newfile.txt', 'r')
available = file.read().split('\r\n')[0]
used = file.read().split('\r\n')[1]
print file.read()
Hope this Helps!
I'm building an ecommerce site with django, and i wanted to create a button that would signal that an order had already been delivered.
I'm pretty sure you can acheive this with a checkbox, but i wanted to use a button, because it would be easier to click when using a tablet.
I want the button to also be "unclickable" in case someone accidentally clicked a button for a wrong order.
Do i need to make a form in the html? or is there an easier way to do it.
this is my html:
Order page
<table>
<tr>
<td>#</td>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
<td>Order</td>
<td>Order Quantity</td>
<td>Delivered</td>
</tr>
{% for ord in orders %}
{% for food in ord.orderitem_set.all %}
<tr>
{% if forloop.counter == 1 %}
<td>{{ord.pk}}</td>
<td>{{ord.user.first_name}}</td>
<td>{{ord.user.email}}</td>
<td>{{ord.user.get_profile.phone}}</td>
{% else %}
<td colspan="4"></td>
{% endif %}
<td>{{food.name}}</td>
<td>{{food.quantity}}</td>
<td>{% if forloop.counter == 1 %} <button type="button">Delivered</button> {% endif %}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
</body>
</html>
hmm, I think just putting a form around the button would be the "easier way" ^^
later if you wanted to, you could make it a spiffy javascript toggle using almost the same view code below maybe
e.g. around your buttons...
<form action="{% url show_orders %}" method="post">
<input type="hidden" name="order-id" value="{{ ord.pk }}"/>
<input type="hidden" name="action=" value="toggledelivery"/>
<button type="button">{% if not ord.is_delivered %}Not {% endif %}Delivered</button>
</form>
then in your view, something like...
def show_orders(request):
if request.method == "POST":
order_id = request.POST.get('order-id', None)
# TODO toggle the order here
return HttpResponseRedirect(back_to_the_order_admin_page)
else:
# ...show the admin page