【问题标题】:How can I fix Django error MultiValueDictKeyError如何修复 Django 错误 MultiValueDictKeyError
【发布时间】:2015-03-14 09:18:18
【问题描述】:

我尝试登录用户,但出现此错误:MultiValueDictKeyError at / "'username'"。我关注了 django 文档:https://docs.djangoproject.com/en/1.7/topics/auth/default/#django.contrib.auth.decorators.login_required

观看次数:

def home(request):

    return render_to_response('home.html', {}, context_instance=RequestContext(request))


def login_user(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('start.html')
        else:
            return HttpResponseRedirect('profile.html')
    else:
        return HttpResponseRedirect('home.html')

网址:

 url(r'^$', 'core.views.login_user', name='login_user'),

html:

<form action="/login_user" method="POST" name="auth">
  {% csrf_token %}
  <label>Email</label>
  <input type="text" name="username">
  <label>Password</label>
  <input type="password" name="password">
  <button type="submit">Login</button>
</form>

【问题讨论】:

  • 你关注的是login_required函数。但似乎你得到了不同的错误,然后给出完整的回溯会更好地调试......!

标签: django login


【解决方案1】:

This question 可能会帮助你:

使用 MultiValueDict 的 get 方法。这也存在于标准 dicts 中,是一种获取值的方法,同时在不存在时提供默认值。

username = request.POST.get("username", False)
password = request.POST.get("password", False)

【讨论】:

    【解决方案2】:

    我发现您的代码中有很多错误。

    您将表单操作指向 /login_user,并且在您的 URL 中您没有定义任何 /login_user,因此当您进入 root / 时,它将加载 login_user 函数。

    我建议你这样做:

    将您的视图更改为以下内容:

    def login_user(request):
        if request.user.is_authenticated():
            return HttpResponseRedirect(reverse('home'))
        if request.method == 'POST':
            form = AuthenticationForm(data=request.POST)
            if form.is_valid():
                usuario = request.POST['username']
                clave = request.POST['password']
                acceso = auth.authenticate(username=usuario, password=clave)
                if acceso is not None:
                    if acceso.is_active:
                        login(request, acceso)                        
                        return HttpResponseRedirect(reverse('home'))
                    else:
                        form = AuthenticationForm()
                        script = "alert('Usuario no activo');"
                        return render(request, 'login.html', locals())
                else:
                    form = AuthenticationForm()
                    script = "alert('Usuario y/o contraseña invalida');"
                    return render(request, 'login.html', locals())
        else:
            form = AuthenticationForm()
        return render(request, 'login.html', locals())
    

    在您的模板中 (login.html)

    <form action="{% url "login" %}" method="post" accept-charset="utf-8">
        {{ form }}
        {% csrf_token %}
        <input class="btn btn-default" type="submit" value="Iniciar Sesión" />
    </form>
    

    在你的 urls.py 中:

    url(r'^$', 'core.views.home', name='home'),
    url(r'^login/$', 'core.views.login_user', name='login'),
    

    这样会显示一个漂亮的表格;)

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 2022-12-12
      • 2011-08-19
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2017-07-17
      相关资源
      最近更新 更多