I'm using Django and I am having trouble passing variable information back to the server without modifying the url. Here's what I have which works the way I want except that I want the action to be /foo:
<form method="post" action="/foo/{{ variable }}">
<input type="submit" value="bar"/>
</form>
When I do this I can easily get the variable when I parse the url. Instead, I want to add the variable to the POST QueryDict. Is this possible?
Yes. just add additional input fields:
<form method="post" action="/foo/">
<input type="hidden" value="{{ variable }}" name="name_of_var">
<input type="submit" value="bar"/>
</form>
Then you can access the variable in the view by:
request.POST['name_of_var']
or if you are not sure if the variable exists. This will not raise exception is does not exist.
request.POST.get('name_of_var')
Just make an input inside your form:
<input type="hidden" value="{{ variable }}" name="foo" />
Then on a view:
foo = request.POST.get('foo', None)
Also, I recomend you to use url tag for action.
Simply add a hidden input element with the name and value you need like so:
<input type="hidden" name="varname" value="myvalue" />
Can you not make the variable an input field in the form? You can make it hidden to avoid overriding it. This will post the data in the submit and achieve what you want.
Isn't this what the Django sessions framework is for? You can pass data back and forth without trying to pass it through a url.
The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies.
https://docs.djangoproject.com/en/1.5/topics/http/sessions/
Related
I have a django template which has multiple <a> tags.
<a class="label label-success" href="get_status/?token={{book.token}}">Update</a>
On click of it, a method from views is called where I can access the token from the url as
tkn = request.GET.get('token')
But now I want not to send the token in the url.
I searched for this and get to know about forms but I did not clearly understand them. Can anyone please help here.
For future ref: I created a form and added a hidden input field in it.
on click of submit button it will send the token value.
<form action="get_Status/" method="post">
{% csrf_token %}
{{ form }}
<input type="hidden" name="book_token" value="{{book.token}}">
<input type="submit" class="submit_btn btn label-success" value="Update" />
</form>
Ans in the views.py
book_token=request.POST.get("book_token"," ")
You can use the basic HTML form concept here.
Please check the link:
How to submit a form with JavaScript by clicking a link?
Use javascript/Jquery to submit the form.
Insert the token value in a hidden field and use form to submit it to views.
Then in the views,you can get the value as :request.POST['token']
I have an url like /documents/search?q=searchterm. On that page, I have an dropdown box where another searchterm can be added. That box has an onchange event and reloads the page:
<form><select name="w" class="form-control" onchange="this.form.submit();">
When the onchange event fires, the page is reloaded and the w parameter is added to the url, but the q parameter is lost. How can I tell my form, that on reload all the existing parameters are kept?
You need to include any other parameters in the form as hidden inputs:
<form>
<select name="w" class="form-control" onchange="this.form.submit();">
<input type="hidden" name="q" value="{{ search_term }}">
</form>
You need to make sure that search_term (obtained from the GET parameters) is in the context being passed to the template.
If possible you should consider using Django's Forms API to manage the form data.
In django, I'm having problems passing the number that was inputed into a form to an URL:
I try like this:
templates->index.html
<form action="/mysite/{{ number }}/details.html" method="post">
<p><label for="number">Give me a number:</label>
<input type="text" name="number" id="number" /></p> <input type="submit" value="Submit" />
but the result is /mysite//details.html, instead of /mysite/123/details.html (when the user inputs 123 in the requested form)
Can you please give me an advise?
Thanks,
JJ
You shouldn't be configuring your action url in this manner. And the user POST-ed number 123 is passed in to your view function as request.POST['number'] without you specifying it in the action url.
Your action url can simply be {% url 'send_details' %} corresponding to a url definition that is
url(r'^mysite/details/$',
'send_details',
name='send_details'),
And your send_details view function will receive request.POST['number'] when the user submits it.
There's no necessity to have the number in your template as a context variable.
I try to post value of input buttons in Django but I couldn't
This is my template
<form id="ReviewRateForm" method="post" action="/review/post/rate/">
<input type="button" hint="V1" title="V" value="1" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
<input type="button" hint="V1" title="V" value="2" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
<input type="button" hint="V1" title="V" value="1" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
</form>
However, when I debug it I couldn't reach the values of that input buttons in my view.
What is the problem or how can I overcome it?
The values can be accessed by the name of the input from request.POST. However, you're dynamically naming the inputs, which is going to make things more complicated when you go to retrieve those values.
Example without taking into consideration the dynamic naming:
quid1 = request.POST.get('quid1')
The problem might be with your browser rather than with django.
If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute.
Update: Oh, you are not using <button> elements, I read too fast. Sorry. Then this answer is not relevant.
I need to take a user-entered value from a form-field and append it as a URL parameter when the user hits {ENTER} or clicks the submit button. I'm new to DreamWeaver CS5, and I have a project that I'm getting pressure to complete.
I can think of three four ways to do it, and only one is two are a ColdFusion solution.
If you use method="get" in the FORM tag, all of the form fields and their values will be appended to the action URL in typical key=value pairs.
The second involves using JavaScript to change the value of the action attribute of the FORM tag in response to an onclick event (or perhaps onsubmit).
A ColdFusion method would to use CFLOCATION to redirect to the desired location. In other words, you want to end up on page2.html, so have the form action be page1.cfm and have in that CF template <cflocation url="page2.html?urlvar=#form.formvar#">.
Updated (courtesy of Ben Doom): On the form's action page, you could use the StructAppend() function to add the URL scope to the FORM scope.
Can you expand on your question a bit? Maybe we can get you a more complete answer.
Like #Al Everett said,
Only need to change method = "get" instead of "post" in form attribute.
Your simple HTML code should look like:
<html>
<head></head>
<body>
<cfif structKeyExists(form,"submit")> <!--- To ckeck if form is submiited --->
<cfoutput>Value = #url.mytextbox#</cfoutput>
</cfif>
<form name="myform" method="get" action=""> <!--- action is blank to submit on same page --->
<input type="text" name="mytextbox" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>