Django why request.method=='GET' when refreshing the page - django

I want to have some search boxes on my page and my related codes are like below, my problem is why when i refresh the page the if clause: " if request.method=='GET':" executes without i click on any button ?
def allstocks_view(request):
if request.method=='GET':
question_set =Stocks.objects.all().filter(confirm=_('approved') )
name=request.GET.get('namesearch')
number=request.GET.get('numbersearch')
brand=request.GET.get('brandsearch')
if name is not None :
question_set = question_set.filter(name__icontains = name)
if number is not None :
question_set = question_set.filter(number__icontains = number)
if request.GET.get("brandsearch"):
question_set = question_set.filter(brand__icontains = brand)
print(">>>>>>>>>>>>>>>>>>>>")
print(question_set)
template :
<form method="get">
{% csrf_token %}
<div class="">
<label for="namesearch">Name</label>
<input type="text" name="namesearch" >
<label for="numbersearch"> Number</label>
<input type="text" name="numbersearch" >
<label for="brandsearch"> Brand</label>
<input type="text" name="brandsearch" >
<label for="brandsearch"> City</label>
<input type="text" name="citysearch" >
<input type="submit" name="search" value="Search">
</div>
</form>

A refresh of a page is a GET request unless your last action was a POST request so that is going to execute every time. What you could do is make the form a post and handle in a request.method == 'POST' block. Another option if you wanted to continute with GET would be have your view take an optional parameter such as search=None and set up your urls accordingly. Then in your view you could check for if search exists instead of request.method == 'GET'.

Related

Form data isn't submitted to the server

The following is all I have for the client server
<div>
<form action="/next/" role="form" method="POST">
{% csrf_token %}
<textarea class="form-control" rows="10"></textarea>
<input type="submit" value="submit"/>
</form>
</div>
and from the server(django) I just want to process the form.
def next(request):
request.body // nothing here except CSRF
request.POST // nothing here except CSRF
It's embarrassing, but what am I doing wrong?
It is not embarrassing. you just forgot name attribute in form element that you want to catch in backend.
<textarea name="element_name" class="form-control" rows="10"></textarea>
and in views.py
def next(request):
if request.method == "POST":
textarea_value = request.POST.get('element_name') # viola!
You forgot to add name attribute to the text area:
<textarea name="some_name" class="form-control" rows="10"></textarea>
Add a "name" attribute to the textarea ?

django authenticationform not showing all errors

My login works fine except for not showing all errors. When i type an invalid username or password, these errors don't show and the page just refreshes without putting any errors down.
However when i leave one field blank, it shows the correct error:
So the missing errors are(from source):
error_messages = {
'invalid_login': _("Please enter a correct %(username)s and password. "
"Note that both fields may be case-sensitive."),
'inactive': _("This account is inactive."),
}
my code:
def login_user(request):
"""Logs a user into the application."""
auth_form = AuthenticationForm(None, request.POST or None)
# The form itself handles authentication and checking to make sure password and such are supplied.
if auth_form.is_valid():
(request, auth_form.get_user())
return HttpResponseRedirect(reverse('index'))
return render(request, 'login.html', {'auth_form': auth_form})
My template:
<form action="{% url 'login_user' %}" method="post" class="login">{% csrf_token %}
<div>
<input name="username" placeholder="Username:" type="text" name="username" value="" id="username" class="login">
{{ auth_form.username.errors }}
</div>
<div>
<input name="password" placeholder="Password:" type="password" name="password" value="" id="password" class="login">
{{ auth_form.password.errors }}
</div>
<div>
<center>
<a href="{% url 'register_user' %}">
register
</a>
<button type="submit" class="link">
login
</button>
</center>
</div>
</form>
what do i do wrong?
You aren't including form.non_field_errors in your template. See the docs on customizing the form template for an example.
As an aside, the AuthenticationForm takes the request as its first argument. It looks slightly strange that you are passing None instead of request.

Django identifying non field validation error is associated with which form

Is there a way to identifying the validation error is associated with which form for a template contaning multiple forms?
Let me explain the scenario. I have login window popup script within my base.html
<div id="dialog1" class="window">
<form method="post" action="/accounts/login/?next=/IW/home" id='login-form' name=login-form>{% csrf_token %}
<div class="d-header">
{{ form.non_field_errors }}
<input type="text" name="username" id="id_username" value="" onclick="this.value=''"/><br/>
<input type="password" name="password" id="id_password" value="" onclick="this.value=''"/><br/>
<input type="hidden" name="login_form" value="1" />
<input type="submit" value="login" />
</div>
{% endif %}
</div>
</form>
</div>
<div id="mask"></div>
{% if form.non_field_errors %}
<script>
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
$('#mask').show();$('.window').show();
</script>
{% endif %}
As all other templates extends base,html whenever there is an error non_field error then login window pops up . I would like to show the login error only when login form is submit not on submit of someother form with a non field error.
For this I need to identify the name of the form.something like this {% ifequal form.form_name login_form %} - Display login error .Is this possible??
They isn't anything special about the name 'form' in the template. It's just a default context name, you can choose to name your forms anything you like.
Just name the forms differently in your context:
from django.http import Http404
def detail(request, poll_id):
# NOTE: This view code isn't really form processing code, just using it to show
# how you can pass different names into your template context.
login_form = MyLoginForm()
detail_form = MyDetailForm()
try:
p = Poll.objects.get(pk=poll_id)
except Poll.DoesNotExist:
raise Http404
return render_to_response('polls/detail.html', {'poll': p, 'login_form': login_form, 'detail_form': detail_form})
And then:
<div id="dialog1" class="window">
<form method="post" action="/accounts/login/?next=/IW/home" id='login-form' name=login-form>
{% csrf_token %}
<div class="d-header">
{{ login_form.non_field_errors }}
.
.
.
Also, if you want to do multiple instances of the same form type, have a look at formsets

"Key 'itemsForRemove' not found in <QueryDict: {}>"

Temlate:
<form action="/remove_from_cart/" method="post">{% csrf_token %}
{% for item in request.session.cart %}
<div><input type="checkbox" name="itemsForRemove" value="{{ item.position }}"/>{{ item.product.name }}</div>
{% endfor %}
<p><input type="submit" value="Delete item"></p>
</form>
views.py
def RemoveProductFromCart(request):
removeThis = request.POST['itemsForRemove']
listOfItems = request.session['cart']
for i in removeThis:
del listOfItems[int(removeThis) - 1]
return redirect(request.path_info)
HTML in browser
<form action="/remove_from_cart/" method="post">
<div style="display:none">
<input type="hidden" name="csrfmiddlewaretoken" value="c49716c88a56e8e9884b31a233076b48"/>
</div>
<div>
<input type="checkbox" name="itemsForRemove" value="1"/>
Some text
</div>
<div>
<input type="checkbox" name="itemsForRemove" value="2"/>
Some text
</div>
<div>
<input type="checkbox" name="itemsForRemove" value="3"/>
Some text
</div>
<div>
<input type="checkbox" name="itemsForRemove" value="4"/>
Some text
</div>
<div>
<input type="checkbox" name="itemsForRemove" value="5"/>
Some text
</div>
<p>
<input type="submit" value="Delete item"/>
</p>
</form>
Raise exception: "Key 'itemsForRemove' not found in "
if I change "removeThis = request.POST['itemsForRemove']" to some think like: removeThis = request.POST['foo'], raise exception: "Key 'foo' not found in ". u'itemsForRemove': [u'6', u'7']} why?! And how i can this fix?
Your problem is that you are redirecting via request.path_info, which is just goint to redirect you back to your remove_cart view.
The reason your first error is "Key 'itemsForRemove' not found in " is because the first attempt worked and your view redirected to itself as GET, which clearly didn't have the itemsForRemove POST data.
When you changed your code to "foo", it's failing on your first POST stage (which is why you properly see "itemsForRemove").
Anyways, fix your redirect problem, then add a check to make sure your view is being called via POST.
def RemoveProductFromCart(request):
if not request.method == 'POST':
return http.HttpResponseForbidden()
removeThis = request.POST['itemsForRemove']
listOfItems = request.session['cart']
for i in removeThis:
del listOfItems[int(removeThis) - 1]
return redirect('somewhere_else')
If the request doesn't include the value for itemsForRemove it will raise an exception. Better way to do this is to use removeThis = request.POST.get('itemsForRemove', ''), it will give a default of '' and no exception is raised even if value doesn't exist.

Django template nt displaying in drop down box and view is showing error

you can view full source code here dpaste.com/hold/167199
Error:
delete() takes exactly 2 arguments (1 given)
Copied from linked code:
index.html
............................................
<form method="POST" action="/customer/(?P<name>[a-z]*)/delete/">
<div style="float: right;
margin: 0px; padding: 05px; ">
<label for="id_customer">Customer:</label>
<select name="customer" id="id_customer">
<option value="" selected="selected">---------</option>
<option value="{{ customer.customer_name|escape }}"></option>
</select>
<input type="submit" value="delete">
</div>
</form>
......................................
Urls.py
(r'^customer/(?P<name>[a-z]*)/delete/', 'quote.excel.views.delete')
Views.py
def delete(request, name):
if request.method == "POST":
Customer.objects.get(name=name).delete()
This is how, i am using it.First,select should display the values presented in db into drop down box but it is rendering dd box,values are empty.
In views,i get 2 params needed only 1 given and problemwith urls.py is 404.
You are mixing the usage of GET and POST requests. You have to do the following:
Either use GET requests, then you have to change your template this way:
<form method="GET" action="/customer/{{customer.customer_name}}/delete/">
<input type="submit" value="delete">
</form>
The name must be part of the URL, because you have set up your urls.py this way. I don't recommend this way, as everybody can just type the URL customer/foo/delete into the address bar to delete customer foo.
The other way is to use post. Therefore you have to change your URL pattern and the view:
(r'^customer/delete/', 'quote.excel.views.delete')
def delete(request):
if request.method == "POST":
name = request.POST.get('customer', False)
if name:
Customer.objects.get(name=name).delete()
But as it seems that you can only delete one customer, there is no need to create a select input element as it only contains one value.
Update:
To make this for all customers, you have to get all of them in your view, e.g. in a variable customers and pass this to the template. In the template, you iterate over all of them:
<form method="POST" action="/customer/delete/">
<label for="id_customer">Customer:</label>
<select name="customer" id="id_customer">
<option value="" selected="selected">---------</option>
{% for customer in customers %}
<option value="{{ customer.customer_name|escape }}">{{ customer.customer_name|escape }}</option>
{% endfor %}
</select>
<input type="submit" value="delete">
</form>
As for the part Django template nt displaying in drop down box I don't know what you mean with it, maybe you can clarify what you want.