python - django 中的身份验证

标签 python django authentication

我正在尝试在我的 django 网站上连接表单

如果在 shell 中,我会这样做:

$ ./manage.py shell
Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib import auth
>>> user = auth.authenticate(username='user', password='password')
>>> 

如你所见,没问题

但如果按照我的观点,我有:

# views.py
...
from django.contrib import auth
...

def check_pass(request):
    ...
    user = auth.authenticate(username='user', password='password')
    ...

我有一个带有堆栈跟踪的 500 错误页面:

Environment:

Request Method: POST
Request URL: http://localhost/check_pass/
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/mart/programmation/python/django/martfiles/views.py" in check_pass
  67.         user = auth.authenticate(username='user', password='password')
File "/usr/lib/python2.6/site-packages/django/contrib/auth/__init__.py" in authenticate
  37.             user = backend.authenticate(**credentials)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/backends.py" in authenticate
  18.             user = User.objects.get(username=username)
File "/usr/lib/python2.6/site-packages/django/db/models/manager.py" in get
  120.         return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in get
  300.         num = len(clone)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in __len__
  81.                 self._result_cache = list(self.iterator())
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in iterator
  238.         for row in self.query.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py" in results_iter
  287.         for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py" in execute_sql
  2369.         cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py" in execute
  19.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py" in execute
  193.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /check_pass/
Exception Value: no such table: auth_user

我的设置.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
...
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)

我已经在没有任何更改的情况下运行了“syncdb”,我确信这是这一行的问题,因为我用

assert False
user = auth...

断言错误

user = auth...
assert False

没有这样的表auth_user

最佳答案

这对我有用:

在脚本的顶部:

from django.contrib.auth import authenticate, login, logout

身份验证:

 user = authenticate(username=request.POST['username'], password=request.POST['password'])
        if user is not None:
            if user.is_active:
                login(request, user)
                #user is loged in
            else:
                # user is not active
        else:
            # false authentification

我希望它对你有用:)

关于python - django 中的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080854/

相关文章:

带有 jquery-tinymce 图片上传插件的 Django

python - django 模板中的元组列表

Microsoft Edge (Spartan) 中的 Windows 身份验证不提示输入凭据

javascript - SPA和SSO中无状态身份验证的性能(单点登录)

iphone - 如何通过 iPhone 对网站用户进行身份验证

python - 线程在调用 Thread.start 之前开始运行

python - 字典中一个键的多个值

python - 使用rpdb远程调试Python服务器

python - 如何在重新训练时初始化tensorflow.contrib.slim full_connected 层的权重和偏差?

Python unittest 报告通过测试