Passing value from template to django view - html

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

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.

Button submit Form HTML double action (submit and reload)

I have the following html that the button does a normal Save:
<form action="" method="post">
<input type="hidden" value="{{ people }}" name="people">
<input type="hidden" value="{{ number }}" name="number">
<p>{% trans 'Are you sure you want to save people' %} {{ people }} {% trans 'where number is' %} {{ number }}?
</p>
{% buttons %}
<button class="btn btn-default hover-linea" id="cancel" type="button">
{% bootstrap_icon "remove" %} {% trans 'Cancel' %}
</button>
<button type="submit" class="btn btn-default hover-linea">
{% bootstrap_icon "log-out" %} {% trans 'Save' %}
</button>
{% endbuttons %}
What I need when I do the Save, is to reload the previous page to the form. I want to press Save and do the same as now, but also make a location.reload() of a page whose relative link is (../people.html).
I await an answer, thank you very much.
Your server-side form handler (which you have specified the URL to in your action) attribute should respond with an HTTP redirect (status 302 and the Location: header) to the URL you want the browser to load.

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.

Redirect back after login django-registration-redux

I am using django-registration-redux and i have a login form in my navbar. I would like to stay on the same page after login. ie. if i am at mypage.com/polls/example after login i want to be back on mypage.com/polls/example not on url set in settings.
Login from in html looks like this:
{% url "auth_login" as login_url %}
{% if login_url not in request.get_full_path %}
<li>
<form class="navbar-form" method="POST" action={{ login_url }}>{% csrf_token %}
<input type="text" class="form-control top-bar" name="username" placeholder="email" />
<input type="password" class="form-control top-bar" name="password" placeholder={% trans "password" %} />
<button type="submit" class="btn btn-default">{% trans "Login" %}</button>
</form>
</li>
{% endif %}
How do i do that?
You could use next :
<form class="navbar-form" method="POST" action="{{ login_url }}?next={{request.path}}">
This will add a GET request to your form that points back to the current page.
For request.path to work you have to define template context processors in your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)

Django template form within a table increasing the row height

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.