无法通过POST.get将django中的变量从模板传递到views.py.

时间:2014-02-20 02:11:29

标签: python html django python-2.7

我正在尝试通过从login.html获取用户名和密码来构建验证函数,然后再与数据库(sqlite)进行比较。但它总是转到invalid.html('account / invalid')。所以我试着用

进行调试
user - auth.authenticate(username='usertest',passowrd='passwordtest')

它运作得很好。所以我想我可能有问题从html页面发送值到view.py请帮助我在这里谢谢!   这是我在view.py中的代码

def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/loggedin')
    else:
        return HttpResponseRedirect('/accounts/invalid')

以下是我在login.html中的代码

    {% extends "default.html" %}
{% block content %}
        {% if form.errors %}
        <p class="error">Sorry. That's not valid user or password</p>
        {% endif %}

        <form action="/accounts/auth/"medthod="post">{% csrf_token %}
        <label for="username">Username:</label>
        <input type="text" name="username" value="" id="username">
        <label for="password">Password:</label>
        <input type="password" name="password" value="" id="password">
        <input type="submit" value="login" />
        </form>

{% endblock %}

更新:这是login.html的拼写错误。无论如何,我仍然不知道究竟是什么。

{% extends "default.html" %}
{% block content %}

  {% if form.errors %}
    <p class="error">Sorry, that's not a valid username or password</p>
  {% endif %}

  <form action="/accounts/auth/" method="post">{% csrf_token %}
    <label for="username">User name:</label>
    <input type="text" name="username" value="" id="username">
    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password">

    <input type="submit" value="login" />

  </form>

{% endblock %}

0 个答案:

没有答案