Django: Toggle active users - python

here in this project, i want to show the active bar when admin is logged in and show blue color and when any other user is logged in, i want to show the active bar on it and others on red color when the users are not active.
This is my html
<div class="card-body table-responsive p-0">
<table class="table table-hover text-nowrap" id="rooms">
<thead>
<tr>
<th>SN</th>
<th>Users</th>
<th>Email</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for user in object_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{user.username }}</td>
<td>{{user.email}}</td>
<td>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td>
<span ><a class="btn btn-info mr-2" href="{% url 'dashboard:passwordreset' %}"
><i
class="fa fa-edit m-1"
aria-hidden="true"
></i>Reset Password</a
></span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
Here I want to show green when admin is logged in and other active when other user is logged in

I do not know what do you mean : active or inactive user. But there is authenticated user, or anonymous user in django. So you can do this:
{% if user.isauthenticated %} your css red or blue {% endif %}

Related

How do I pass the value from the inside of a for loop ( jinja2 ) to a bootstrap modal to display it in side the modal body?

How do I pass the value from the inside of a for loop ( jinja2 ) to a bootstrap modal to display it in side the modal body?
here is my views.py file:
if request.method == 'GET':
driver = get_network_driver(device.napalm_driver)
with driver(device.IP_address, device.username, device.password) as device_conn:
interfaces = device_conn.get_interfaces()
context = {
'device': device,
'interfaces': interfaces,
}
return render(request, 'network/interface_list.html', context)
Please note that the the device_conn.get_interfaces() method returns a nested dictionary.
Here is the html template:
{% extends 'base/base.html' %}
{% block title %}iNet Interface List{% endblock %}
{% block content %}
<div class="section">
{% include 'network/include/interface_list_header.html' %}
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success' %}<div class="alert alert-success" role="alert">
{{ message }}
</div>{% elif message.tags == 'error' %}<div class="alert alert-danger" role="alert">
{{ message }}
</div>{% endif %}
{% endfor %}
{% endif %}
<div class="card mx-auto shadow rounded">
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col" style="text-align:center">Interface Name</th>
<th scope="col" style="text-align:center">Admin status</th>
<th scope="col" style="text-align:center">Protocol</th>
<th scope="col" style="text-align:center">Description</th>
<th scope="col" style="text-align:center">MAC Address</th>
<th scope="col" style="text-align:center">Last Flapped</th>
<th scope="col" style="text-align:center">Turn On/Off Port</th>
<th scope="col" style="text-align:center">Edit Description</th>
</tr>
</thead>
<tbody>
{% for interface_name, interface in interfaces.items %}
<tr>
<form action="{% url 'get_interfaces' device.id %}" method="post" id="{{ interface_name }}">
{% csrf_token %}
<input type="hidden" value="{{ interface_name }}" name="interface_name" />
<input type="hidden" value="{{ interface.is_enabled|yesno:'False,True' }}" name="enable" />
</form>
<td style="text-align:center">{{ interface_name }}</td>
<td style="text-align:center">{% if interface.is_enabled %}<i class="fa-solid fa-check text-success"></i>{% else %}<i class="fa-solid fa-xmark text-danger"></i>{% endif %}</td>
<td style="text-align:center">{% if interface.is_up %}<i class="fa-solid fa-check text-success"></i>{% else %}<i class="fa-solid fa-xmark text-danger"></i>{% endif %}</td>
<td style="text-align:center">{{ interface.description }} </td>
<td style="text-align:center">{{ interface.mac_address }}</td>
<td style="text-align:center">{{ interface.last_flapped }}</td>
<td style="text-align:center"><button class="btn common-button btn-primary" type="submit" form="{{ interface_name }}" value="Submit">{% if interface.is_enabled %}Turn Off{% else %}Turn On{% endif %}</button></td>
<!-- Modal Trigger button -->
<td style="text-align:center"><i class="fa-regular fa-clipboard"></i></td>
<!-- Modal container -->
<div class="modal fade viewModal" id="editDesc{{ interface_name }}" tabindex="-1" role="dialog" aria-labelledby="editDescLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 style="font-size: 20px;" class="modal-title" id="editDescLabel">View Console Output</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<textarea>{{ interface.description }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}
I wish to show the {{interface.description}} of the selected interface in the textarea so that I can edit it later. Any help would be appreciated.
My issue is - I am able to fetch {{interface.description}} outside the bootstrap modal, however I am unable to fetch the same thin inside the bootstrap modal. Eventhough both of them are inside the for loop.

Expected pk value, received str (pk is a CharField)

Hi i am new to django and html and I am making a shopping CRUD project with models Producst,categories,Sub_categories,size,colors using SERIALIZERS.
I am now trying to make crud of categories,and while inserting I am getting the following error:
"{'category_name': [ErrorDetail(string='Incorrect type. Expected pk value, received str.', code='incorrect_type')]}"
below is my show_cat function
def show_cat(request):
showcategory = Categories.objects.filter(isactive=True)
#print(showall)
serializer = CategoriesSerializer(showcategory,many=True)
#print(serializer.data)
return render(request,'polls/show_cat.html',{"data":serializer.data})
insert_cat function
def insert_cat(request):
if request.method == "POST":
insertcategory = {}
insertcategory['category_name']=request.POST.get('category_name')
insertcategory['category_description']=request.POST.get('category_description')
form = CategoriesSerializer(data=insertcategory)
if form.is_valid():
form.save()
print("hkjk",form.data)
messages.success(request,'Record Updated Successfully...!:)')
return redirect('categories:show_cat')
else:
print(form.errors)
return redirect('categories:show_cat')
else:
return render(request,'polls/insert_cat.html')
below is html for insert
<tr>
<td>Category Name</td>
<td>
<input type="text" name="category_name" placeholder="CATEGORIES">
</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea name="category_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
below is the showcategory html page
{% for result in data %}
<tbody>
<tr>
<td><b>{{result.category_name}}</b></td>
<td><b>{{result.category_description}}</b></td>
<td style="position: relative;left:50px;">
<a href="categories/edit_cat/{{result.id}}">
<button class="btn btn-primary">
<i class="fa-solid fa-pen-to-square">EDIT</i>
</button>
</a>
</td>
<td>
<a href="{% url 'categories:del_cat' result.id %}" onclick="return confirm('Are You Sure you want to delete?')">
<button class="btn btn-danger">
<i class="fa-solid fa-trash">DELETE</i>
</button>
</a>
</td>
</tr>
</tbody>
{% endfor %}
</table>
Where am I going wrong in the code

How to set notification system in Django?

I am trying to create a notification system with Django. I have an approval process in my project. A user approves a document according to his/her role. For example manager sent a document to the regional manager and wait for the approval. When the manager sends the document it goes to the regional manager's pending approvals list.
I created these tables. What I want to is When the user's pending approval is found, write it in the notification icon above. How can I do it?
Note: The notification bar in base.html
There is a picture for understanding clearly.
views.py
def approval(request):
current_user = request.user
rank_priority = RankPriority.objects.filter(rank = current_user.rank)
priority = rank_priority[0].priority
pend_list = ApprovalProcess.objects.filter(status = priority )
submit_list = ApprovalProcess.objects.filter(user_id = current_user)
context = {
'pend_list': pend_list,
'submit_list': submit_list
}
return render(request, 'approvals.html', context)
approvals.html
<div class="content">
<div class="page-inner">
<div class="page-header">
<h4 class="page-title">Approvals</h4>
</div>
<div class="row">
<div class="col-md">
<div class="card">
<div class="card-body">
<ul class="nav nav-pills nav-secondary" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Pending Approvals</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Submitted Approvals</a>
</li>
</ul>
<div class="tab-content mt-2 mb-3" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
<div class="card">
<div class="card-title">Pending Approvals</div>
<div class="card-body">
<table class="table table-head-bg-primary mt-4">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">User</th>
<th scope="col">Document ID</th>
<th scope="col">Beginning Date</th>
<th scope="col">End Date</th>
<th scope="col">Approve</th>
</tr>
</thead>
<tbody>
{% for pend in pend_list %}
<tr>
<td>1</td>
<td>{{ pend.last_approved.username }}</td>
<td>{{ pend.doc_id }}</td>
<td>{{ pend.begin_date }}</td>
<td>{{ pend.end_date }}</td>
<td>Approve</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
<div class="card">
<div class="card-title">Submitted Approvals</div>
<div class="card-body">
<table class="table table-head-bg-primary mt-4">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">User</th>
<th scope="col">Document ID</th>
<th scope="col">Beginning Date</th>
<th scope="col">End Date</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
{% for submit in submit_list %}
<tr>
<td>1</td>
<td>{{ submit.last_approved.username }}</td>
<td>{{ submit.doc_id }}</td>
<td>{{ submit.begin_date }}</td>
<td>{{ submit.end_date }}</td>
{% if submit.status > submit.highest_rank %}
<td>Approved</td>
{% else %}
<td>Waiting</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The easiest way to do it is to create and register your custom context processor. For each request, your own template context processor will recalculate the state of notifications.
def approval_context_processor(request):
current_user = request.user
rank_priority = RankPriority.objects.filter(rank = current_user.rank)
priority = rank_priority[0].priority
pend_list = ApprovalProcess.objects.filter(status = priority )
submit_list = ApprovalProcess.objects.filter(user_id = current_user)
context = {
'pend_list': pend_list,
'submit_list': submit_list
}
return context
On your settings:
TEMPLATES = [
{
'# ...,
'OPTIONS': {
'context_processors': [
#...,
'approval_context_processor',
],
},
},
]
At this point, pend_list and submit_list will be available on all templates.

The "Else if" statement in my Django templates appears after the HTML header block

I have some data which I am passing from my views to my templates, whenever the else if the statement is true(that is the if the statement is false) the statement appears after my HTML header().
My main issue is that why is the else block not under the HTML heaader where I have put it in the code
templates.html
<table class="table table-borderless table-data3">
<thead>
<tr>
<th>No</th>
<th>Email</th>
<th>Telephone</th>
<th>Country</th>
<th>Status</th>
<th>Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% if supplier %}
{% for supplier in suppliers %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{supplier.email}}</td>
<td>{{supplier.telephone}}</td>
<td>{{supplier.country}}</td>
<td class="process">Active</td>
<td>{{supplier.image.url}}</td>
<td>
<div class="table-data-feature">
<button class="item" data-toggle="tooltip" data-placement="top" title="Edit">
<i class="zmdi zmdi-edit"></i>
</button>
<button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="zmdi zmdi-delete"></i>
</button>
</div>
</td>
</tr>
{% endfor %}
{% else %}
<h2>No supplier available</h2>
{% endif %}
</tbody>
</table>
You can not write a h2 in <tbody> or at least not directly. Yolu write this wrapped in a <tr> and <td>:
{% if suppliers %}
…
{% else %}
<tr><td colspan="7">No supplier available</td></tr>
{% endif %}
You also made a typo in the if statement: it is suppliers, not supplier.
Note: Django has a {% for … %}…{% empty %} template tag [Django-doc] that
can be used to render a message if the collection you iterate over is empty.
You can use the {% for … %}…{% empty %} django template tag. Example:
HTML
{% for supplier in suppliers %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{supplier.email}}</td>
<td>{{supplier.telephone}}</td>
<td>{{supplier.country}}</td>
<td class="process">Active</td>
<td>{{supplier.image.url}}</td>
<td>
<div class="table-data-feature">
<button class="item" data-toggle="tooltip" data-placement="top" title="Edit">
<i class="zmdi zmdi-edit"></i>
</button>
<button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="zmdi zmdi-delete"></i>
</button>
</div>
</td>
</tr>
{% empty %}
<div><h2>No Suppliers Available</h2></div>
{% endfor %}

How can i reduce the table width or make the table responsive for chrome?(Django/Python deployed web-app)

This is the code for the page that is being viewed in those images, is there any changes that need to be made to make it more responsive in a mobile chrome browser?
<!---To Display Timetable Entries----->
<div class="container"><br>
<span style="font-family:'Futura';font-weight:bold;"><h2>TimetableMatch</h2></span>
<hr>
<div style="overflow-x:auto;">
<table class="table table-dark">
<thead>
<tr>
<th>Module</th>
<th>Day</th>
<th>Time</th>
<th>Area</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entry in entrys %}
<tr>
<td>{{ entry.module }}</td>
<td>{{ entry.day }}</td>
<td>{{ entry.time }}</td>
<td>{{ entry.location }}</td>
<td><a class="btn btn-info" href="../edit/{{ entry.id }}">Edit</a>
<br>
<br>
<a class="btn btn-danger" href="../delete/{{ entry.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-4 col-lg-2">
<a class="btn btn-primary" href="../add_entry/">Add Timetable Entry</a>
<br>
<br>
<a href="{% url 'homepage' %}">
<button type="button" class="btn btn-primary btn-xs"> Back to home</button></a>
<a href="{% url 'index' %}">
<br>
<br>
<button type="button" class="btn btn-primary">Logout</button> </a>
</div>
</div>
</body>
IOS chrome browser
Laptop chrome browser

Categories