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
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>
I have a for loop within my Django project, what I'am trying to do is the following :
If
morning_recess == True
lunch_recess == True
afternoon_recess == True
then the bootstrap tag in that field should be
<td><span class="badge badge-success">Success</span></td>
else
<td> None </td>
Here is my current code:
<table style="width:100%">
<tr>
<th>Student Name</th>
<th>Morning Recess</th>
<th>Lunch Recess</th>
<th>Afternoon Recess</th>
<th>Earned At</th>
</tr>
<tr>
{% for i in students_recess_today %}
{% if i.morning_recess == True %}
<td>{{i.student_ps }}</td>
<td><span class="badge badge-success">Success</span></td>
<td>{{i.lunch_recess}}</td>
<td>{{i.afternoon_recess}}</td>
<td>{{i.created_at}}</td>
{% else %}
<td>{{i.student_ps }}</td>
<td>None</td>
<td>{{i.lunch_recess}}</td>
<td>{{i.afternoon_recess}}</td>
<td>{{i.created_at}}</td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
The morning_recess works fine, however if i do another if statement after the following one, the order of my table gets all messed up. How do I write this correctly? Thank you
It's not clear what "in that field" means, because in your example you have one extra column before the morning_recess column. But you can put {% if %} statements anywhere you want in the template, e.g.:
<td>
{% if i.morning_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
<td>
{% if i.lunch_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
<td>
{% if i.afternoon_recess %}
<span class="badge badge-success">Success</span>
{% else %}
<span>None</span>
{% endif %}
</td>
...
Also as other commenters suggest, your for loop should probably wrap the rows of your table (<tr>...</tr>), not the columns (<td>).
You have the loop partially inside your tr element. It starts a row at the beginning and for every student adds the columns and ends a row, so it ends up looking like <tr></tr></tr></tr>.
You should move your for statement outside of the row, like so:
...
{% for i in students_recess_today %}
<tr>
...
</tr>
{% endfor %}
</table>
</div>
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.
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 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.