So I'm rendering a template, including a table...and for one of the columns, the value is blank or contains a Delete button/form (depending on whether delete is possible).
Anyway, my issue is that if the Delete button is present the row height is twice that of the others, making my table look rather ugly.
{% if condition %}
<td>
<form name="input" action="./delete/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="submit" name={{item}} value="Delete">
<input type="hidden" name="filter_start" value={% if date1 %}{{date1}}{% endif %}>
<input type="hidden" name="filter_end" value={% if date2 %}{{date2}}{% endif %}>
</form>
</td>
{% else %}
{% endif %}
Is there anything clearly wrong with the above? The "hidden" bits are so that I can retrieve certain 'dates' with the 'delete' button.
Try to add style="display:inline;" on the form.
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 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 am trying to post all checked input checkboxes. The input field is generated by django's for loop as shown below.
From what I have found so far, the below should be working. I believe the issue may be in the fact that the input fields are generated through the forloop, if so, how can I get around this? For each value add to list and post with js?
index.html
{% for q in list %}
{% if forloop.last %}
<form method="POST" name="selectedchecks"> {% csrf_token %}
<div class="qblock">
<label class="pure-material-checkbox">
<input class="selectedchecks" type="checkbox" name="choices[]" value="{{ q }}">
<span>
<p>{{ q }}</p>
</span>
</label>
</div>
</form>
{% endif %}
{% endfor %}
views.py
if request.method == 'POST':
selected_list = request.POST.getlist('choices[]')
What happens is that only the first value of {{ q }} is returned if the first checkbox is selected, if any other checkbox apart from the first is selected, nothing is returned (empty list). Selecting all checkboxes also only returns the first value.
It should POST all selected checkbox values.
Any help is appreciated!
UPDATE
The problem was that the form was being re-initialized for every loop iteration. Setting the <form> tag before the loop fixed the issue!
I have a template in which I display some values. Now I want to be able to change that values. So I have an input field with the value "capacity.capacity". The user can now change this value in the input field and click on button "Change".
Now I want to process the newly entered value in my django view. How can I pass the new value to my view?
I tried to add the value in the url so that I can retrieve it in my view again.
Is it possible to add the new value to the url or is there a different/better way to do this?
<td>
<input type="number" name=new_capacity id="new_capacity" step="1" value= {{ capacity.capacity }}>
<button>
<span class="glyphicon glyphicon-edit"></span>
<a href="{% url 'change_capacity' item_id=capacity.pk new_capa=NEWVALUE %}"> Change
</button>
</td>
Update: Full code
I have two forms in this template and two submit buttons. Both buttons should redirect to different views. However it seems that always the same view is called.
{% block body %}
<form action="/producer/capacity/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Hinzufügen" />
</form>
<br/>
<br/>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Maschine</th>
<th>Verfügbare Kapazität</th>
</tr>
</thead>
<tbody>
{% for capacity in capacity_list %}
<tr>
<td>{{ capacity.pk }}</td>
<td>
<form action="" method="post">{% csrf_token %}
<input type="number" name=new_capacity id="new_capacity" step="1" value="{{capacity.capacity}}" />
<input type="submit" value="Change"/>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
I think it's better to use POST method to make changes in the model data instead of using GET, so you can write a form like this in your template:
<form action="{% url my_app.views.my_capacity_view %}" method="post">{% csrf_token %}
<input type="number" name=new_capacity id="new_capacity" step="1" value="{{capacity.capacity}}" />
<input type="submit" value="Change" />
</form>
You can set the action form url in order to send the field to a custom view to modify the capacity value. Then, in the view, you can get the value and make the change, for example:
def my_capacity_view(request):
if request.method == 'POST':
new_capacity = request.POST.get('new_capacity ', '')
# set new value 'new_capacity' to the object
Set the url to point to the view
(r'^newcapacity', 'my_app.views.my_capacity_view'),
I hope this was useful
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.