I am trying to develop some basic django templates where i initially pass the django template variable which is an array - {{array_list}}. I am able to perform operations on this dtv easily. But I am unable to post this variable back to views.
Eg. I pass {'array_list': [1,2,3,4]}
<form action="some_action" method="post">
{% csrf_token %}
<input type="submit" value="sort">
<input type="hidden" name="array_list" value={{array_list}}>
</form>
And in views.py:
array_list = request.POST['array_list']
return render(request, 'result.html', {'array_list': array_list})
But i don't get the full array back to result.html, and i only get [1, as the array_list.
Probably you can do something like this:
First, use join tag to turn that list into comma separated string.
<form action="some_action" method="post">
{% csrf_token %}
<input type="submit" value="sort">
<input type="hidden" name="array_list" value='{{array_list|join:","}}'>
</form>
And get the value in POST request and split it by ,.
array_list = request.POST['array_list'].split(',')
Related
I am trying to spin first Django project and I am struggling how to pass value to python function from html. Basically, I have 1 placeholder with 3 buttons next to it.
I have file.html in templates.
<form method="POST" action="">
{% csrf_token %}
<input type="text" name="name_id" placeholder="placeholder value"/>
<input type="submit" value="Value 1"/>
<input type="submit" value="Value 2"/>
<input type="submit" value="Value 3"/>
</form>
I am trying to pass placeholder value to views.py. In views.py I have 3 functions. I want to execute only one of them based on value.
Value 1 from file html triggers function 1 in views py
Value 2 from file html triggers function 2 in views py
Value 3 from file html triggers function 3 in views py
I am not sure how to handle urls.py
All files are in same app/folder
Any help is much appreciated.
You can work with a name="…" attribute on the submit <button>s:
<form method="POST" action="">
{% csrf_token %}
<input type="text" name="name_id" placeholder="placeholder value"/>
<input type="submit" name="value1"/>
<input type="submit" name="value2"/>
<input type="submit" name="value3"/>
</form>
In the view you can then check with:
def some_view(request):
if request.method == 'POST':
if 'value1' in request.POST:
# …
pass
elif 'value2' in request.POST:
# …
pass
elif 'value3' in request.POST:
# …
pass
This is my html form in which i have a text field for a word and i am running a for loop which created checkbox for the list of document a user has and displays it as form.
<body>
<form id="form" method = "POST">
{% csrf_token %}
<fieldset id="User">
<legend>Your details:</legend>
<p><label for="word">Enter your word</label>
<input type="text" id="word" name="word">
</p>
{% for doc in docs %}
<p>
<input type="checkbox" id="{{ doc.document_id }}" name="docid" value="{{ doc.path }}">
<label for="{{ doc.document_id }}"> {{ doc.document_name }} </label><br>
</p>
{% endfor%}
<input type="submit" value="Submit" >
</fieldset>
</form>
</body>
In views.py I have below method which loads this html page
def client_home(request):
client_pr = Client_Profile.objects.get(user_id=request.user)
events = Event.objects.filter(client_id=client_pr.pk)
name = request.POST.get('word')
id = request.POST.get('docid')
print(name)
print(id)
docs = []
for eve in events:
docs = Document.objects.filter(event_id=eve)
context = {'events': events, 'docs': docs}
return render(request, 'Elasticsearch/form.html', context)
My question is as i am using a for loop create checkbox field based on number of documents a user has and when I try to print them in my Views.py file it is only priniting last document id whose checkbox is created and not all the documents.
From seeing your view, I assume you want to pass a list of documents specific to a user to your template. Currently you are overwriting docs in every iteration of your for loop.
Change
docs = Document.objects.filter(event_id=eve)
to
docs += Document.objects.filter(event_id=eve)
You need to use getlist to get all the selected values from your checkbox, so:
id_list = request.POST.getlist('docid')
Otherwise using get will only return the last one.
I am use Django 1.8 and Python 3.5.2, and try recive POST data from simple html form
<form action="/shop/order" method="POST">
{% csrf_token %}
<input type="hidden" name="product-code" value="{{ product.product_code }}">
<input type="text" name="email">
<input type="submit">
</form>
It view where I try get need for me data, but QueryDict empty for POST, when I change POST to GET in form and view all work.
def order_product(request):
test = request.POST.get('product-code', '')
mail = request.POST.get('email', '')
# logger.info(test)
return render(request, 'shop/test.html', dict(test=test, email=mail))
And I'am not understand why is that. Similarly I try parse(decode binary to utf-8 etc) body and I recive empty string.
urls.py
url(r'^order/?$', views.order_product),
Thanks all, the moral of this fable is as follows, use name argument in urls, and use {% url 'name' %} in template. I have i18n in urls(from djangoCMS) and when I harcode url in form isn't work for POST method.
So, Im trying to save a todo to a database with django. My template is as follows:
<form action="{% url 'todo:add' %}" method="POST">
{% csrf_token %}
<input type="text" id="text" value="{{new_todo_text}}"/>
<input type="submit" value="Submit todo" />
</form>
{%if not_done_todos %}
<ul>
{%for todo in not_done_todos%}
<li>
<span>{{todo.text}}</span>
</li>
{%endfor%}
</ul>
{%else%}
<span>No todos for you!</span>
{%endif%}
My view where Im trying to catch the "new_todo_text" looks like this:
def add(request):
"""Add todo to database"""
new_todo = Todo(text=request.POST.get('new_todo_text'),
done=False, date_created=timezone.now())
new_todo.save()
return HttpResponseRedirect(reverse('todo:index'))
The problem is that the todo.text turns up empty no matter what I add to the form... whats my problem here?
The problem is that inside your input element you have not declared a name attribute. That name attibute will be used as a key to fetch it with Django request.POST.
So, change to this:
<input type="text" id="text" name="new_todo_text" value="{{new_todo_text}}"/>
The request.POST dict-like will use the input's name and value to build the dict. Then you can do stuff like request.POST['input_name_here'].
Hi I got a simple form for a POST request and it works when I'm only having one input, but not two inputs together. Can someone show me some light on this?
index.html
<form name="input" action="{% url 'sending' %}" method="post">
{% csrf_token %}
Recipient: <input type="text" name="recipient">
<br>
Message: <input type="text" name="content">
<br>
<input type="submit">
</form>
views.py
def sending(request):
recipient = request.POST.get('recipient','')
content = request.POST.get('content','') #not working when I am doing this...
someMethod(recipient, content)
return HttpResponseRedirect(reverse('results'))
Adding a "forms" portion to your setup will help you greatly... see the quickstart docs on forms here: https://docs.djangoproject.com/en/1.6/topics/forms/
In particular, check out "using a form in a view": https://docs.djangoproject.com/en/1.6/topics/forms/#using-a-form-in-a-view
Basically, you end up with a "forms.py" file which defines your form fields. Then, after it all processes, you get a simplier API into your form fields that looks like this:
form.cleaned_data['recipient']
form.cleaned_data['content']
etc.