54. Creating a team and a page to manage the store
We will now implement functionality to create multiple user access levels for the website.
In the admin panel's "Users" tab, each user is listed with their staff status and associated permissions.


Additionally, in the left column of the admin page, there is an option labeled "Groups."

Click the "Add" button in the "Groups" row and create a group named Team, as shown below:

Now, in the "Users" tab, select a specific user and navigate to their "Group" section. You can assign the Team group to that user.

Important note: Remember to click the save button in the bottom of the admin page everytime you perform a change.
We can use these groups to create specific pages on the website.
To achieve this, we'll define a function inside the new_context.py
file to check if a user belongs to a specific group. This function is placed in new_context.py
since it will be used across all pages in the navbar.
def is_part_of_team(request) :
team = False
if request.user.is_authenticated:
if request.user.groups.filter(name="Team").exists(): #? if the user is authenticated and the group Team is created (parameters obtained from admin page)
team = True
return {"Team" : team}
Hereโs a explanation of the code:
Function Name:
is_part_of_team
Parameters: Accepts a
request
object, typically provided by Django during an HTTP request.Purpose: Checks if the currently authenticated user is part of the group named "Team."
Logic Flow:
Initializes a variable
team
toFalse
.Verifies if the user is authenticated by checking
request.user.is_authenticated
.If the user is authenticated, it checks if the user belongs to a group named "Team" using
request.user.groups.filter(name="Team").exists()
.Sets
team
toTrue
if the user is part of the "Team" group.
Return Value: Returns a dictionary with the key
"Team"
and the boolean value ofteam
.
Since we created a new_context
function that will be available across all pages, add a reference to this function in the settings.py
file at the bottom of the TEMPLATES
list, as previously done.
'store.new_context.is_part_of_team',
],
},
Now, in the navbar.html
file, we will check if the user belongs to the Team group using the context function we created. If the user is part of the group, a link to the administration page will be displayed.
This verification should be added right after the existing check for user authentication:
{% if request.user.is_authenticated %}
<a href = "{% url 'your_account' %}">My Account</a>
{% else %}
<a href = "{% url 'perform_login' %}">Login</a>
{% endif %}
{% if Team %}
<a href="{% url 'manage_store' %}">Manage Store</a>
{% endif %}
To create a new view page, add the corresponding path to the urls.py
file in the store folder.
path('managestore/', manage_store, name="manage_store"),
Note: You may arrange the views in any order you prefer. However, to prevent issues, follow the sequence provided in the source code on the GitHub repository.
Next, add a view function named manage_store
at the end of the views.py
file.
@login_required
def manage_store(request):
if request.user.groups.filter(name="Team").exists():
return render(request,"internal/manage_store.html")
else:
redirect('store')
Inside the function we added the filter so that when a user that is not inside the Team group enters the manage store option, he will instead be redirected to the store.
Within the templates
folder, create a subfolder named internal
to house all internal HTML files used for website management.
Finally, within the internal
folder, create the manage_store.html
file.
{% extends 'base.html' %}
{% load static %}
{% block body %}
<h3>
Manage store
</h3>
{% endblock %}
When accessing the website with an account in the Team group, the "Manage Store" option will appear in the navbar. Clicking this option will redirect you to the manage_store.html
page.

Last updated