简体   繁体   English

Python-Django-activate()返回无

[英]Python - Django - activate() returns None

Okay, so I'm trying to create a log in with Django. 好的,所以我试图用Django创建一个登录。

from views.py: 从views.py:

from django.shortcuts import *
from forms import UserRegistrationForm
from django.contrib.auth import authenticate, login
from django.core.mail import send_mail
from django.conf import settings
import pprint
import utils
import models
from django.http import HttpResponse
import views
from django.conf.urls import patterns, url    

def login_user(request):
    state = "Please log in below..."
    username = password = ''

    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)

        if user is not None:
            if not user.is_verified:
                state = "Please check your e-mail to verify your account."
            elif user.is_active:
                login(request, user)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

        return render_to_response('myapp/dashboard.html', {'state': state}, RequestContext(request))

    else:
        return render_to_response('myapp/login.html', RequestContext(request))

My login template (login.html): 我的登录模板(login.html):

<h3>Login</h3><hr />

            <form action="/login/" method="post">
        {% csrf_token %}
                {% if next %}
                <input type="hidden" name="next" value="{{ next }}" />
                {% endif %}

                username:
                <input type="text" name="username" value="{{ username }}" /><br />
                password:
                <input type="password" name="password" value="" /><br />

                <input class="btn" type="submit" value="Log In" />
            </form>

I'm using a basic SQLite3 to get everything figured out. 我正在使用基本的SQLite3弄清所有内容。 python manage.py syncdb runs with no problems python manage.py syncdb运行没有问题

from settings.py: 来自settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'tempDB'
    }
}

When I log into my admin site I see the user: 当我登录到管理站点时,我看到该用户:

username: nick password: the hashed version of nick 用户名:昵称密码:昵称的哈希版本

When I insert a break (using PBD) I was able to see that the app goes through the login_user view. 当我插入一个中断(使用PBD)时,我可以看到该应用程序通过了login_user视图。

> ...myapp/myapp/views.py(52)login_user()
-> state = "Please log in below..."
(Pdb) n
> ...myapp/myapp/views.py(53)login_user()
-> username = password = ''
(Pdb) print username
*** NameError: name 'username' is not defined
(Pdb) n
> ...myapp/myapp/views.py(55)login_user()
-> if request.POST:
(Pdb) print username

(Pdb) n
> ...myapp/myapp/views.py(56)login_user()
-> username = request.POST.get('username')
(Pdb) print username

(Pdb) n
> ...myapp/myapp/views.py(57)login_user()
-> password = request.POST.get('password')
(Pdb) print username
nick
(Pdb) n
> ...myapp/myapp/views.py(59)login_user()
-> user = authenticate(username=username, password=password)
(Pdb) print password
nick
(Pdb) n
> ...myapp/myapp/views.py(61)login_user()
-> if user is not None:
(Pdb) print user
None

This shows that it gets the username 'nick' and the password 'nick' (the settings that I set the account up with) however the problem I run into is that authenticate is still returning None which means SOMETHING is not set up correctly / is not working. 这表明它获得了用户名“ nick”和密码“ nick”(用于设置帐户的设置),但是遇到的问题是身份验证仍返回None,这意味着未正确设置/不工作。

After that login_user view the 'state' prints "Your username and/or password were incorrect." 在该login_user视图之后,“状态”显示“您的用户名和/或密码不正确”。 as told to do in views.py 就像在views.py中告诉的那样

I would greatly appreciate it if someone would be able to help me out! 如果有人能够帮助我,我将不胜感激!

(Replied on G+ as well) (也适用于G +)

From your pdb session: 在您的pdb会话中:

(Pdb) print password
nick

If that's what happens, there's a problem there, don't you agree? 如果发生这种情况,那就是一个问题,您不同意吗? Also, I'd do away with the initialization of the vars and do this instead: 另外,我会取消vars的初始化,而是这样做:

username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
    # Your code

request.POST.get will work anyway (POST is always initialized as a QueryDict) and the get() returns None if the key is not found, so you won't get any errors - and it might be easier for you to see what's going on. request.POST.get仍然可以正常工作(POST始终被初始化为QueryDict),如果未找到键,则get()返回None,因此不会出现任何错误-而且可能更容易查看内容继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM