3
我是Django的新手。我使用內置的User
型號編寫了登錄腳本。從Django登錄檢索SQL條目
def login_user(request):
state = "Please login below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return render_to_response('home.html',{'username': username})
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('index.html',{'state':state})
這將打開一個新頁面home
哪裏顯示{{username}}
。 但是,如何從database
中檢索id
,email
,0 fullname
等特定username
的其他值,並將其顯示在我的home
頁面上。我嘗試過使用User.objects.all()
,但它似乎不起作用。
大它的工作原理 – xan
@xan它。可能值得一提的是,django可以將用戶作爲變量提供給使用內置(在設置中啓用時)內容處理器的所有模板:https://docs.djangoproject.com/en/dev/ref/templates/api/#django -contrib-auth-context-processors-auth –
@xan:傳入整個'user'對象,並訪問temp中的方法和屬性遲到,或者使用Josh指出的上下文處理器。要查詢文章,請使用Django的標準查詢API。 –