![](/img/trans.png)
[英]Display the objects from all users in django without login on html page
[英]django login users and display their instance and items in the database
我有幾個模型,其中一個是擴展django管理員的用戶模型的外鍵。 我希望在登錄時顯示其會話中屬於用戶的內容。 我已經定義了此身份驗證,該身份驗證將檢查數據庫中是否存在特定用戶,並將其重定向到與其實例的會話。
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect('/studentloggedin/')
基本上,Registration是Student模型的第一個模型和外鍵,而Student也是UserLog的外鍵。 UserLog正在擴展默認的django admin。 我在這里定義了登錄會話,以便在登錄時過濾掉各個用戶的詳細信息。
def studentloggedin(request):
registration = Registration.objects.all()
students = Student.objects.filter(registration=registration)
alluser = UserLog.objects.filter(student=students)
context = {
'registration': registration,
'students': students,
'alluser': alluser,
}
return render(request, "studentloggedin.html", context)
這是在登錄時呈現信息的模板。
<img
{% for student in students %}
src="{{ student.student_photo.url }}">
<p>{{ student.previous_school }}</p>
{% endfor %}
但我得到以下錯誤:
編程錯誤/ studentloggedin /
用作表達式的子查詢返回的多行
我認為你正在做django已經有的事情:
if request.user.is_authenticated:
# Do something for authenticated users.
...
else:
# Do something for anonymous users.
...
從這里: https : //docs.djangoproject.com/en/1.10/topics/auth/default/#authentication-in-web-requests
你也可以使用裝飾器@login_required
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.