Chidananda Nayak
Chidananda Nayak

Reputation: 1201

request.POST.get() is giving None

class ApiLoginView(TemplateView):
    template_name = 'index.html'

    def post(self,request):
        email = request.POST.get('Email')
        password = request.POST.get('Password')
        print(email)
        print(password)

        API_KEY = GetAPIkey().api_key_token()
        API_URL = GetAPIurl().api_url_token()
        parameter = {
            'authToken':API_KEY,
            'email':email,
            'password':password,
        }
        url = '{}{}'.format(API_URL,'/rest/storeLogin')
        r = requests.post(url = url, params=parameter)
        print(r.url)
        print(request.user)
        return HttpResponse(r)

I am trying to get data from request.POST method, but it is sending me NONE value everytime when i print email and password. Where am i wrong?Am i doing ajax call wrong? when i was using form submi for ajax calling it was working fine but now i use button click for ajax call.

<form class="my-login-form"  id="sanjh_login_form" method="post" url="/login">
                                  {% csrf_token %}
                                <div id="login-data"></div>

                                <div class="field-wrap">
                                <input id='id_login-email' type="email" name="Email" required autocomplete="off" placeholder="Email Id">
                              </div>
                                <div class="field-wrap">
                                <input id='id_login-password' type="password" name="Password" required autocomplete="off" placeholder="Password">
                              </div>
                                  <button id="login-btn" type="button" class="button button-block" >Login</button>
                                <div class="forgot"><a class="user-form-toggle" href="#forgot">Forgot Password?</a></div>
                              </form>

ajax call

<script>
$(document).ready(function(){
   $('#login-btn').click(function(event){
        console.log('hi-login')

        $.ajax({
            method: "POST",
            url: '/login',
            data: {
            email : $('#id-login-email').val(),
            password : $('#id-login-password').val(),
            csrfmiddlewaretoken:'{{ csrf_token }}'
            },

            success: function(res) {
            var response = $.parseJSON(res)
                console.log(response.msg)
                      $('#login-data').html(response.msg);
                    },

        })

    })

})

</script>

Upvotes: 0

Views: 7626

Answers (4)

Shadeer
Shadeer

Reputation: 89

I got that problem in when using input search

search = request.POST.get('search')

this returned me a "None"

<input type='text'/>

request.POST.get('search') | you should add a "name" attribute to your input to use this POST.get() method

<input type='text' name = 'search' /> 

soo simple...

Upvotes: -1

R.A.M
R.A.M

Reputation: 102

In my case request.POST.get('value') not work.
But request.data.get('value') worked.
Try this.

Upvotes: 3

dirkgroten
dirkgroten

Reputation: 20672

Rather than giving an answer with code, I'll tell you how to debug such issues:

  1. Since the values are None you can conclude that your ajax call doesn't pass keys "Email" and "Password". Indeed, when I look at your code, your ajax data is set to "email" and "password". That's your first errors.

  2. Next, you printed request.POST and saw that even "email" and "password" aren't passed. Looking at how the values are fetched from your form, I see you're looking for id's "id-login-email" and "id-login-password", which don't exist in your HTML (you have "id_login-email" and "id_login-password"). So there's your second mistake.

  3. If there are more issues, try setting breakpoints in your javascript (e.g. on the line $.ajax) and check the values you're assigning using the console, e.g. type $(#id_login-email).val() in your console to check it's the correct value.

  4. Finally, always print or add a breakpoint in your post() method to view the contents of request.POST, it's the best way to understand what the client is actually submitting.

Upvotes: 5

Umair Mohammad
Umair Mohammad

Reputation: 4635

Kindly fix your id attribute value of email and password text fields and retry.

<input id='id-login-email' type="email" name="Email" required autocomplete="off" placeholder="Email Id">
<input id='id-login-password' type="password" name="Password" required autocomplete="off" placeholder="Password">

and also key value in your view

email = request.POST.get('email')
password = request.POST.get('password')

Once you make these working try refactoring to use request.data instead of request.POST

Upvotes: 1

Related Questions