View未从表单接收POST数据

时间:2015-09-30 07:51:20

标签: python django

我的观点是没有收到我从登录表单发送的任何POST数据。

要测试提交,我想将电子邮件字段输出到浏览器。

从视图返回的输出是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)

1 个答案:

答案 0 :(得分:5)

输入标签中没有名称字段。

将您的HTML更改为类似的内容...请注意带有输入标记的name属性,这是最重要的。

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