我想要一種方法來檢查是否有人用django填寫了他們的個人資料信息(新用戶)。如果他們不想展示一種在所有信息填寫完畢之前都不會消失的模式。無論他們進入哪個頁面,它都應該顯示此模式直到填寫完畢。確定新網站用戶的最佳方法 - Django
我應該使用javascript(ajax)來檢查一個路線,該路線將使檢查並返回一個帶有答案的json請求?如果json對象說他們是新的,我會動態地將模態附加到屏幕上。
更新:我使用django的身份驗證系統。這裏是一個登錄的例子。該檢查將是類似的,但我將使用另一個擴展了Django基本用戶類的應用程序中創建的模型。我稱之爲user_profile。我可能會檢查是否設置了用戶的名字。如果不是,我會想執行檢查。
def auth_login(request):
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
request.session['id'] = user.id
request.session['email'] = user.email
login(request, user)
data = {}
data['status'] = "login"
return HttpResponse(json.dumps(data), content_type="application/json")
#redirect
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.")
data = {}
data['status'] = "The username and password are incorrect"
return HttpResponse(json.dumps(data), content_type="application/json")
return HttpResponse("hello")
有這樣做的許多方面。你可以提供更多的上下文來縮小答案的範圍。你有一個認證系統,即你可以檢查'request.user.is_authenticated()'嗎? – YPCrumble
也許提供你的視圖代碼,因爲這是最有可能發生這種檢查的地方。 – YPCrumble
@YPCrumble我用一個例子更新了這個問題。是的,我使用Django的身份驗證系統。 –