Django - add parameter to url - django

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.

Related

Custom `password_reset_confirm.html` template won't redirect to `password_reset_complete` view

I made a custom password_reset_confirm.html template. But when a user enters a new password and hits submit, the browser does not redirect to the admin view password_reset_complete.
Here's the form I made in the custom password_reset_confirm.html template:
<div ng-app="app" ng-controller="Ctrl">
<form id="reset-pw-confirm-form" name="newPWForm" method="post" action="">
{% csrf_token %}
<input
id="id_new_password1"
type="[[[ newPW.showPW ? 'text' : 'password' ]]]"
name="new_password1"
ng-model="newPW.pw"
ng-minlength="8"
ng-maxlength="32"
required>
<button class="btn btn-primary" type="submit" ng-disabled="!newPW.pw">Submit</button>
<input
id="id_new_password2"
type="hidden"
value="[[[ newPW ]]]"
name="new_password2"
ng-model="newPW"
ng-minlength="8"
ng-maxlength="32"
required>
</form>
</div>
When I fill out the password and hit submit, the browser sends a POST request to the same URL it landed on, but the page seems to just refresh with nothing changed. The user's password remains unchanged. It seems Django's auth/views.py did not execute properly.
In that view, there's this code:
if post_reset_redirect is None:
post_reset_redirect = reverse('password_reset_complete')
else:
post_reset_redirect = resolve_url(post_reset_redirect)
When I have the view print post_reset_redirect, it prints None. Could this be the issue?
How can I make my custom template compatible with Django's password_reset_confirm view?
When you specifies "action" attribute for the form it will be used as your link for data sending so probably your logic isn't handled. Try to remove it and check you js files that the data is sent to the the specified link.
Also please check all required parameters for the password_reset_confirm
https://docs.djangoproject.com/en/1.8/_modules/django/contrib/auth/views/
My hidden input's value and ng-model attributes needed to be set to newPW.pw.

How to send data from django template to views method in post request or in a way data is not visible in the URL

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']

create a html form with 2 action pags

How can i create a html form that action attribute have 2 destination.I want when user click on submit bottom , check if user entered wrong data the page goes to another pages with window.location and if user insert the correct input goes to main page with the same instruction.
First of all, what do you mean by correct input?
Main form data validation occurs in server side, not client side. you'd better use client side just for simple verification, like for typos.
There is no need for 2 destination pages (as you call it so).
You may use the standard action attribute which is the page on the server to which you are sending your form data.
there, You have the option to decide which condition needs what action and send the data (and then the user) to the desired page / action.
Sample code for the form
<form id='myform' action='action.php' method='POST' target='formresponse'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' maxlength="50" /><br/>
<label for='email' >Email Address*:</label><br/>
<input type='text' name='email' id='email' maxlength="50" /><br/>
<input type='button' name='Submit' value='Submit' />
</form>
<iframe name='formresponse' width='300' height='200'></frame>
Multiple action
function SubmitForm()
{
document.forms['contactus'].action='action1.php';
document.forms['contactus'].target='frame_result1';
document.forms['contactus'].submit();
document.forms['contactus'].action='action2.php';
document.forms['contactus'].target='frame_result2';
document.forms['contactus'].submit();
return true;
}

Django: passing form input to the url

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.

Additional POST data for django

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/