2010-01-17 72 views
2

我試圖做我的Django的網站連接形式認證在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 

我settings.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... 

AssertionError的隔離它

user = auth... 
assert False 

no such table auth_user

+1

您確定您正在從'manage.py shell'命中同一個數據庫嗎?你打了你的意見?如果你從shell中查看代碼,會發生什麼? – 2010-01-17 11:59:54

+0

是的,也許它是不一樣的(它會解釋),但我怎麼檢查? 「擊中查看代碼」是什麼意思? – 2010-01-17 12:27:54

回答

0

好的,我找到了。與static files相同,我需要將我的數據庫放在我的home文件夾之外,而不是在我的python文件旁邊......

1

這個工作對我來說:

在腳本的頂部:

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 

我希望它會爲你工作,以:)

+0

並且您還沒有其他東西導入?你可以告訴我你的settings.py(也許)?我試過這個非常簡單的視圖(http://paste.pocoo.org/show/166380/),仍然出現錯誤'no such table:auth_user' – 2010-01-17 17:08:32