Franco
Franco

Reputation: 2926

View is not receiving POST data from form

My view is not receiving any of the POST data I am sending from a login form.

To test out the submission, I want to output the email field to the browser.

The output being returned from the view is None

login.html

        <form action="/login_email/" method="POST">
            {% csrf_token %}
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="email" placeholder="Email">
            </div>
            <div class="form-group">
                <label for="email">Password</label>
                <input type="password" class="form-control" id="password" placeholder="Password">
            </div>
            <button type="submit" class="btn btn-info">Submit</button>
        </form>

views.py

def login_email(request):

if request.method == 'POST':
    email = request.POST.get('email')
    password = request.POST.get('password')

    return HttpResponse(email)

Upvotes: 2

Views: 2891

Answers (1)

cafebabe1991
cafebabe1991

Reputation: 5186

There is no name field in your input tags.

Change your html to something like this...Please note the name attribute with the input tag, that's most important.

<form action="/login_email/" method="POST">
        {% csrf_token %}
        <div class="form-group">
            <label for="email">Email address</label>
            <input name="email" type="email" class="form-control" id="email" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="email">Password</label>
            <input name="password" type="password" class="form-control" id="password" placeholder="Password">
        </div>
        <button type="submit" class="btn btn-info">Submit</button>
    </form>

Upvotes: 11

Related Questions