Django 表单总是失败

发布于 2024-10-09 12:29:40 字数 1477 浏览 0 评论 0原文

我正在尝试使用 Django 的内置身份验证模块。对于我正在开发的网站,我想使用电子邮件地址作为登录名,而不仅仅是通常设置的普通字母数字字段。为了做到这一点,我将所有字符串字段更改为电子邮件字段,并将其最大长度从 30 更改为 320。我的注册代码似乎工作正常,但我的登录代码则不然。这是我现在正在使用的:

def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            return HttpResponse("valid")
            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("/")
                    # Redirect to a success page.
                else:
                    return HttpResponse("Disabled Account")
                    # Return a 'disabled account' error message
            else:
                return HttpResponse("Invalid Login")
                # Return an 'invalid login' error message.
        else:
            return HttpResponse("%s" % repr(form.errors))
    else:
        form = AuthenticationForm()

    return render_to_response("login.html",  {'form': form,  }, context_instance=RequestContext(request))

无论我提交什么,form.is_valid()都会返回FALSE,但form.errors是空的。有什么想法可能是错的吗?我想我将所有内容都更改为电子邮件属性,所以我认为不是这样。另外,如果它改变了我尝试使用 djangoappengine 在 Google 应用引擎上执行此操作的任何内容。

I'm trying to use Django's built in authentication modules. For the site I'm working on I want to use email addresses as login names and not just the normal alphanumeric fields they're usually set to. In order to do this I changed all the String fields to Email fields and changed their max length from 30 to 320. My registration code appears to be working fine but not my login code. Here is what I'm using right now:

def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            return HttpResponse("valid")
            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("/")
                    # Redirect to a success page.
                else:
                    return HttpResponse("Disabled Account")
                    # Return a 'disabled account' error message
            else:
                return HttpResponse("Invalid Login")
                # Return an 'invalid login' error message.
        else:
            return HttpResponse("%s" % repr(form.errors))
    else:
        form = AuthenticationForm()

    return render_to_response("login.html",  {'form': form,  }, context_instance=RequestContext(request))

No matter what I submit, form.is_valid() is returning FALSE but form.errors is empty. Any ideas what might be wrong? I think I changed everything over to Email properties so I don't think that's it. Also, in case it changes anything I'm trying to do this on google app engine using djangoappengine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-10-16 12:29:40

抱歉,您无法在 Google 应用引擎之上使用 Django 的身份验证模块。 Django 使用自己的特殊数据库后端,类似于 google-app-engine,但不兼容。

如果您想在 GAE 上进行身份验证,您应该通过 google-app-engine 方式进行:
http://code.google.com/appengine/docs/python/users/

Sorry, but you cannot use Django's authentication module on top of google app engine. Django uses its own special database backend which is similar to google-app-engine's but is not drop-in compatible.

If you want to do authentication on GAE, you should do it the google-app-engine way:
http://code.google.com/appengine/docs/python/users/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文