Biswajit
Biswajit

Reputation: 39

I am always getting none from request.POST.get in django

nam and mob field are userinput fields which i am using for user input and later i am using them for filtering

{% block content %}
<form class="form-signin" action="" method="POST">
        {% csrf_token %}
<div class="mb-3">
        <input type="text" name="nam" id="nam"  class="form-control-sm center-block" placeholder="Nam" autofocus>
    </div>
    </div>  
    <div class="mb-3">                                               
        <select class="custom-select center-block" name="mob" id="mob" >
        <option>{{ customer.sponsor }}</option>
        {% for i in sponsor %}
        <option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
        {% endfor %} 
        </select>        
        <div class="invalid-feedback">
        Please select a valid Existing Customer.
        </div>
    </div>                   
    <div class="mb-3">
            <a href="{% url 'customer_view' %}" class="btn btn-primary btn-sm" role = "button">Search</a>

Urls.py

path('customer_view',views.customer_view,name='customer_view')

Views.py

def customer_view(request):
    print(request.method )  
    name1 = str(request.POST.get('nam'))  
    print(name1) 
    mobile1 = str(request.POST.get('mob'))
    print(mobile1)    
    customers_list = customer.objects.filter(
    mobile=mobile1) & customer.objects.filter(name=name1)
    sponsors = customer.objects.all().distinct('mobile')
    ctx = { 'customer': customers_list, 'sponsor': sponsors } 
    return render(request, 'pages/customer_view.html', ctx)

Upvotes: 1

Views: 57

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477666

In your form, you write a button as:

<a href="{% url 'customer_view' %}" class="btn btn-primary btn-sm" role="button">Search</a>

But this thus means that this is just a link that links to a new page. As a result, the browser will make an (empty) GET request to the given url, and never submit the form.

You can construct a button that submits the form with:

<form class="form-signin" action="{% url 'customer_view' %}" method="post">
    <!-- … -->
    <button type="submit" class="btn btn-primary btn-sm" role="button">Search</button>
    <!-- … -->
</form>

That being said, a search is often done with a GET request, so you might want to change method="get", and obtain the parameters through request.GET.get(..) instead.

Upvotes: 1

Kostas Charitidis
Kostas Charitidis

Reputation: 3113

You use href which does not submit the form. You need a submit button and change your action of your form to your view url. Try this:

{% block content %}
<form class="form-signin" action="{% url 'customer_view' %}" method="POST">
        {% csrf_token %}
<div class="mb-3">
        <input type="text" name="nam" id="nam"  class="form-control-sm center-block" placeholder="Nam" autofocus>
    </div>
    </div>  
    <div class="mb-3">                                               
        <select class="custom-select center-block" name="mob" id="mob" >
        <option>{{ customer.sponsor }}</option>
        {% for i in sponsor %}
        <option value="{{ i.mobile }}"> {{ i.mobile|add:' - '|add:i.name }} </option>
        {% endfor %} 
        </select>        
        <div class="invalid-feedback">
        Please select a valid Existing Customer.
        </div>
    </div>                   
    <div class="mb-3">
            <input type="submit" value="Search"/>
.
.
...

Upvotes: 1

Related Questions