我想用我的mongoengine db在我的Django項目中處理身份驗證。Django - 使用mongoengine進行身份驗證DB
我嘗試了幾個關於這個東西的例子回答了老問題,但它沒有運行。我使用的是Django 1.6和mongoengine。一切都已安裝,正在運行,我可以創建文檔並將其保存到我的Mongoengine數據庫。
我下面http://mongoengine-odm.readthedocs.org/en/latest/django.html
,我得到以下錯誤:
當我運行:
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', '[email protected]', 'johnpassword')
我得到這個:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/REBORN/reb_env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 273, in __get__
self.model._meta.object_name, self.model._meta.swapped
AttributeError: Manager isn't available; User has been swapped for 'mongo_auth.MongoUser'
>>>
我真不」理解2件事:
- 我必須創建並定義用戶將被存儲在哪個數據庫,否則他們將自動創建?
- 什麼是經理?我還沒有定義任何經理的東西
在開始我認爲寄存器被保存在一個分貝。叫做'mongo_auth.MongoUser',但它並沒有將它保存在任何地方。
這裏是機型:
# Create your models here.
from mongoengine import *
class Profile(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
class auth_user(Document):
username = StringField(max_length=50)
email = StringField(max_length=50)
password = StringField(max_length=50)
的settings.py的手冊上說的配置是否正確。
編輯@cestDiego:
我的設置是完全一樣的,我注意到有關DB後端,因爲它創造了我,因爲我用蒙戈...反正我是從尤斯這沒興趣數據庫mongoengine.django.auth導入用戶,但現在當我嘗試創建一個用戶,它返回我:
>>> user = User.objects.create_user('john', '[email protected]', 'johnpassword')
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'create_user'
也許我們定製身份驗證,這就是爲什麼不行,不知道。你也有這個問題嗎?
第二個編輯:
我讀,我們必須使用Django的權威性,後配置正確的設置,既是我們已經做了。
然後必須導入django.contrib.auth導入驗證並使用驗證,因爲它在Django文檔中提供,希望有所幫助; D。
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from game.models import *
from mongoengine import *
from models import User
from django.contrib.auth import authenticate
def login(request):
user = authenticate(username='john', password='secret')
if user is not None:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
我在此行中收到錯誤 user.backend ='mongoengine.django.auth.MongoEngineBackend' 錯誤是'MetaDict'對象有沒有屬性'PK' –