Creating list and detail views for user profiles

Open the views.py file of the account application and add the following code to it:

from django.shortcuts import get_object_or_404from django.contrib.auth.models import User@login_requireddef user_list(request):    users = User.objects.filter(is_active=True)    return render(request,                  'account/user/list.html',                  {'section': 'people',                   'users': users})@login_requireddef user_detail(request, username):    user = get_object_or_404(User,                             username=username,                             is_active=True)    return render(request,                  'account/user/detail.html',                  {'section': 'people',                   'user': user})

These are simple list and detail views for user objects. The user_list view gets all active users. The Django User model contains an is_active flag to ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.