Send an Array via a POST Message in Django - html

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')

Related

How to store a table to database in Django 2.1?

I have a table inside html, and I need to save it into database using view and model and form. Here are some part of the code:
template.html
<form method="post" action="/images/save/" enctype="multipart/form-data">
{% csrf_token %}
<table class="table" border="1" id="tbl_posts">
<tbody id="tbl_posts_body">
{% for name, age in lines %}
{% with i=forloop.counter0 %}
{% with i|add:1|stringformat:"s" as i_id %}
{% with id="rec-"|add:i_id %}
<tr id={{id}}>
<td><span class="sn">{{ i|add:1 }}</span>.</td>
<td><INPUT type="text" name="txt1" value=""\></td>
<td><INPUT type="text" name="txt2" value=""\></td>
</tr>
{% endwith %}
{% endwith %}
{% endwith %}
{% endfor %}
</tbody>
</table>
<input type="submit" value="Submit">
</form>
model.py:
class Names(models.Model):
name= models.CharField(max_length=255)
age= models.IntegerField()
view.py:
def save_form(request):
template = "template.html"
context = {'txt1': "Name", 'txt2': 0}
if request.method == 'POST':
dname= request.POST.get("txt1")
dage= request.POST.get("txt2")
names1= Names(name=dname, age=dage)
names1.save()
return render(request, template, context)
Question:
So, it works perfectly, but the issue is that It saves only the last row. I think there is a way to enter the whole data. I need to enter all data in the table not only the last row. Can someone help me?
Update:
lines is a zip a combination of two lists, I read it from a file.
These two lines are the ones which get sent in a form.
<td><INPUT type="text" name="txt1" value=""\></td>
<td><INPUT type="text" name="txt2" value=""\></td>
However, you are using the same "names" to send the values for multiple rows.The result of that is that you will only get those values once with the current code you have in your view. You will want to give each of them a unique name (just do something like this:
<td><INPUT type="text" name="txt{{forloop.counter}}" value=""\></td>
and then iterate through them in your view.

In Django, how to use a list from html checkboxes to loop through elements, and go to element specific page

I want to obtain a list from a HTML form with specific IDs in it:
search.html:
<form method='POST' action='/report/'>
{% for o in obj %}
<tbody>
<tr>
<td>{{o.sample}}</td>
<td><input type="checkbox" name="Samples" value="{{o.sample}}</td>
</tr>
</tbody>
{% endfor %}
<input type="submit" value="Submit">
</form>
This gives me a list of samples to my report view which takes me to /report/
in my URLs i have defined a regex so that it would be able to take me to a sample specific report URL:
url(r'^report/Sam\d{1,5}_\d{2}/$', views.report),
What I want is to be able to loop through this list sample IDs, so when I click the initial submit, it takes me to the first sample ID at:
localhost:8000/report/H1_1/
then I process and make a report for this sample, I submit and it takes me to the next sample ID in my list:
localhost:8000/report/H2_1/
etc.
I have been racking my brain on how to do this and the only thing I came up with was:
search.html:
{% for o in obj %}
<form method='POST' action='/report/{{o.sample}}'>
<tbody>
<tr>
<td>{{o.sample}}</td>
<td><input type="checkbox" name="Samples" value="{{o.sample}}</td>
</tr>
</tbody>
{% endfor %}
<input type="submit" value="Submit">
</form>
but I cant work how to get to the next sample in the sample specific html page form.
You probably want to close every form inside the loop, or you want a single form with all the entry, but what you have now is not so clean and could lead to errors.
Something like this:
{% for o in obj %}
<form method="POST" action="/report/{{o.sample}}">
<tbody>
<tr>
<td>{{o.sample}}</td>
<td><input type="checkbox" name="Samples" value="{{o.sample}}"></td>
</tr>
</tbody>
<input type="submit" value="Submit" label="Submit {{o.sample}}">
</form>
{% endfor %}
Or like this for a single form:
<form method="POST" action="/report/samples">
{% for o in obj %}
<tbody>
<tr>
<td>{{o.sample}}</td>
<td><input type="checkbox" name="Samples" value="{{o.sample}}"></td>
</tr>
</tbody>
{% endfor %}
<input type="submit" value="Submit" label="Submit">
</form>
But I think, based on what you wrote, that the 1st example better fits your needs.

Flask and html get values from a text file

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!

Using html button to change status of data

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

How to create a recursive table in HTML

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.